简体   繁体   English

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

[英]For loop for files in multiple folders - bash shell

I need to have files from many directories in a for loop.我需要在 for 循环中包含来自许多目录的文件。 As for now, I have the following code:至于现在,我有以下代码:

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提前致谢

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./*/*

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM