如何使用 bash 对目录中的每个文件执行某些操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1310422/
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-09 18:26:30  来源:igfitidea点击:

How to do something to every file in a directory using bash?

bashshell

提问by

I started with this:

我从这个开始:

command *

But it doesn't work when the directory is empty; the * wildcard becomes a literal "*" character. So I switched to this:

但是当目录为空时它不起作用;* 通配符变成文字“*”字符。所以我切换到这个:

for i in *; do
   ...
done

which works, but again, not if the directory is empty. I resorted to using ls:

这有效,但同样,如果目录为空,则无效。我求助于使用 ls:

for i in `ls -A`

but of course, then file names with spaces in them get split. I tried tacking on the -Q switch:

但是当然,然后其中包含空格的文件名会被拆分。我尝试添加 -Q 开关:

for i in `ls -AQ`

which causes the names to still be split, only with a quote character at the beginning and ending of the name. Am I missing something obvious here, or is this harder than it ought it be?

这会导致名称仍然被拆分,仅在名称的开头和结尾处有一个引号字符。我是否在这里遗漏了一些明显的东西,或者这比它应该的更难?

回答by

Assuming you only want to do something to files, the simple solution is to test if $i is a file:

假设你只想对文件做一些事情,简单的解决方案是测试 $i 是否是一个文件:

for i in * 
do
    if test -f "$i" 
    then
       echo "Doing somthing to $i"
    fi
done

You should really always make such tests, because you almost certainly don't want to treat files and directories in the same way. Note the quotes around the "$i" which prevent problems with filenames containing spaces.

您确实应该始终进行此类测试,因为您几乎肯定不想以相同的方式处理文件和目录。请注意“$i”周围的引号,它可以防止文件名包含空格出现问题。

回答by Bombe

findcould be what you want.

find可能是你想要的。

find . | while read file; do
    # do something with $file
done

Or maybe like this:

或者像这样:

find . -exec <command> {} \;

If you do not want the search to include subdirectories you might need to add a combination of -type fand -maxdepth 1to the find command. See the find man pagefor details.

如果不希望搜索不包含子目录,你可能需要添加的组合-type f,并-maxdepth 1以find命令。有关详细信息,请参阅查找手册页

回答by pavium

It depends whether you're going to type this at a command prompt, and which command you're applying to the files.

这取决于您是否要在命令提示符下键入它,以及您对文件应用哪个命令。

If it's typed you could go with your second choice and substitute something harmless for the command. I like to use echo instead of mv or rm, for example.

如果它被输入,你可以选择你的第二个选择并用无害的命令代替命令。例如,我喜欢使用 echo 而不是 mv 或 rm。

Put it all on one line:

将所有内容放在一行中:

for i in * ; do command $i; done

When that works - or you can see where it fails, and whether it's harmless, you can press up-arrow, edit the command and try again.

当它起作用时 - 或者您可以看到它失败的地方,以及它是否无害,您可以按向上箭头,编辑命令并重试。

回答by crafter

Use shopt to prevent expansion to *.txt

使用 shopt 防止扩展为 *.txt

shopt -s nullglob
for myfile in *.txt
do
    # your code here
    echo $myfile

done

回答by Niko

this should do the trick:

这应该可以解决问题:

find -type d -print0 | xargs -n 1 -0 echo "your folder: {} !"
find -type f -print0 | xargs -n 1 -0 echo "your file: {} !"

the print0 / 0 are there to avoid problems with whitespace

print0 / 0 是为了避免空格问题