Linux For 循环多个文件夹中的文件 - bash shell

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

For loop for files in multiple folders - bash shell

linuxbashloopsfor-loop

提问by fanjabi

I need to have files from many directories in a for loop. As for now, I have the following code:

我需要在 for 循环中包含来自许多目录的文件。至于现在,我有以下代码:

for f in ./test1/*;
...
for f in ./test2/*;
...
for f in ./test3/*;
...

In each loop I'm doing the same thing. Is there a way to get files from multiple folders?

在每个循环中,我都在做同样的事情。有没有办法从多个文件夹中获取文件?

Thanks in advance

提前致谢

采纳答案by evgeny

Try for f in ./{test1,test2,test3}/*or for f in ./*/*depending on what you want.

尝试for f in ./{test1,test2,test3}/*for f in ./*/*取决于你想要什么。

回答by Phil

You can give multiple "words" to for, so the simplest answer is:

你可以给多个“词” for,所以最简单的答案是:

for f in  ./test1 ./test2 ./test3; do
  ...
done

There are then various tricks to reduce the amount of typing; namely globbing and brace expansion.

然后有各种技巧来减少打字量;即通配符和大括号扩展。

# the shell searchs for matching filenames 
for f in ./test?; do 
...
# the brace syntax expands with each given string
for f in ./test{1,2,3}; do
...
# same thing but using integer sequences
for f in ./test{1..3}