bash Shell脚本列出给定目录中的文件以及它们是文件还是目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36545025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:14:03  来源:igfitidea点击:

Shell Script to list files in a given directory and if they are files or directories

bashshell

提问by Ant695

Currently learning some bash scripting and having an issue with a question involving listing all files in a given directory and stating if they are a file or directory. The issue I am having is that I only get either my current directory or if a specify a directory it will just say that it is a directory eg. /home/user/shell_scripts will return shell_scipts is a directory rather than the files contained within it. This is what I have so far:

目前正在学习一些 bash 脚本,并且遇到了一个问题,涉及列出给定目录中的所有文件并说明它们是文件还是目录。我遇到的问题是我只能获取当前目录,或者如果指定一个目录,它只会说它是一个目录,例如。/home/user/shell_scripts 将返回 shell_scipts 是一个目录,而不是其中包含的文件。这是我到目前为止:

dir=$dir
for file in $dir; do
    if [[ -d $file ]]; then
        echo "$file is a directory"
    if [[ -f $file ]]; then
        echo "$file is a regular file"
    fi
done

回答by Component 10

Your line:

您的线路:

for file in $dir; do

will expand $dirjust to a single directory string. What you need to do is expand that to a list of files in the directory. You could do this using the following:

$dir仅扩展为单个目录字符串。您需要做的是将其扩展为目录中的文件列表。您可以使用以下方法执行此操作:

for file in "${dir}/"* ; do

This will expand the "${dir}/"*section into a name-only list of the current directory. As Biffen points out, this should guarantee that the file list wont end up with split partial file names in fileif any of them contain whitespace.

这会将"${dir}/"*部分扩展为当前目录的仅名称列表。正如 Biffen 指出的那样,这应该保证文件列表不会以拆分的部分文件名结束,file如果其中任何一个包含空格。

If you want to recurse into the directories in dirthen using findmight be a better approach. Simply use:

如果你想递归到目录中,dir那么使用find可能是更好的方法。只需使用:

for file in $( find ${dir} ); do

Note that while simple, this will nothandle files or directories with spaces in them. Because of this, I would be tempted to drop the loop and generate the output in one go. This might be slightly different than what you want, but is likely to be easier to read and a lot more efficient, especially with large numbers of files. For example, To list all the directories:

请注意,虽然简单,但这不会处理其中包含空格的文件或目录。因此,我很想放弃循环并一次性生成输出。这可能与您想要的略有不同,但可能更易于阅读且效率更高,尤其是在处理大量文件时。例如,要列出所有目录:

find ${dir} -maxdepth 1 -type d

and to list the files:

并列出文件:

find ${dir} -maxdepth 1 -type f

if you want to iterate into directories below, then remove the -maxdepth 1

如果你想迭代到下面的目录,然后删除 -maxdepth 1

回答by Biffen

This is a good use for globbing:

这是globbing 的一个很好的用途:

for file in "$dir/"*
do
  [[ -d "$file" ]] && echo "$file is a directory"
  [[ -f "$file" ]] && echo "$file is a regular file"
done

This will work even if files in $dirhave special characters in their names, such as spaces, asterisks and even newlines.

即使文件名中$dir包含特殊字符(例如空格、星号甚至换行符),这也将起作用。

Also note that variables should be quoted ("$file"). But *must notbe quoted. And I removed dir=$dirsince it doesn't do anything (except break when $dircontains special characters).

还要注意变量应该被引用("$file")。但*一定不能引用。我删除了dir=$dir因为它不做任何事情(除了$dir包含特殊字符时中断)。

回答by agc

ls -F ~ | \
sed 's#.*/$#/& is a Directory#;t quit;s#.*#/& is a File#;:quit;s/[*/=>@|] / /'

The -F"classify" switch appends a "/" if a file is a directory. The sed code prints the desired message, then removes the suffix.

如果文件是目录,-F“分类”开关会附加一个“/”。sed 代码打印所需的消息,然后删除后缀。

回答by Enjoy Red

for file in $(ls $dir)
do
    [ -f $file ] && echo "$file is File"
    [ -d $file ] && echo "$file is Directory"
done

or replace the

或更换

$(ls $dir)

with

`ls $`

回答by TrevTheDev

If you want to list files that also start with . use:

如果要列出也以 . 用:

for file in "${dir}/"* "${dir}/"/.[!.]* "${dir}/"/..?* ; do