2011-05-25 99 views
6

我需要在for循环中有许多目录中的文件。 至于现在,我有以下代码:for循环多个文件夹中的文件 - bash shell

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

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

在此先感谢

回答

12

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

+0

谢谢,这对我有用! – fanjabi 2011-05-25 09:46:59

+0

我的荣幸!你能将这个答案标记为“接受”吗?干杯。 – evgeny 2011-05-25 09:55:23

5

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

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

有那么不同的技巧来减少输入量;即通过扩展和支撑扩展。

# 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}