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

16

我需要在for循环中从多个目录获取文件。 目前,我的代码如下:

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

每次循环我都在做同样的事情。有没有一种方法可以从多个文件夹获取文件?

2个回答

29

根据您需要的结果,尝试使用 for f in ./{test1,test2,test3}/* 或者 for f in ./*/*


20

您可以向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}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接