对于多个文件夹中的文件循环 - bash shell

fan*_*abi 9 linux bash loops for-loop

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

for f in ./test1/*;
...
for f in ./test2/*;
...
for f in ./test3/*;
...
Run Code Online (Sandbox Code Playgroud)

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

提前致谢

evg*_*eny 18

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


Phi*_*hil 13

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

for f in  ./test1 ./test2 ./test3; do
  ...
done
Run Code Online (Sandbox Code Playgroud)

然后有各种技巧来减少打字量; 即globbing和brace扩展.

# 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}
Run Code Online (Sandbox Code Playgroud)