【发布时间】:2023-03-25 01:12:02
【问题描述】:
我需要在 for 循环中包含来自多个目录的文件。 至于现在,我有以下代码:
for f in ./test1/*;
...
for f in ./test2/*;
...
for f in ./test3/*;
...
在每个循环中,我都在做同样的事情。有没有办法从多个文件夹中获取文件?
提前致谢
【问题讨论】:
我需要在 for 循环中包含来自多个目录的文件。 至于现在,我有以下代码:
for f in ./test1/*;
...
for f in ./test2/*;
...
for f in ./test3/*;
...
在每个循环中,我都在做同样的事情。有没有办法从多个文件夹中获取文件?
提前致谢
【问题讨论】:
根据您的需要尝试for f in ./{test1,test2,test3}/* 或for f in ./*/*。
【讨论】:
你可以给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}
【讨论】: