【问题标题】:For loop for files in multiple folders - bash shellFor循环多个文件夹中的文件 - bash shell
【发布时间】:2023-03-25 01:12:02
【问题描述】:

我需要在 for 循环中包含来自多个目录的文件。 至于现在,我有以下代码:

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

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

提前致谢

【问题讨论】:

    标签: linux bash loops for-loop


    【解决方案1】:

    根据您的需要尝试for f in ./{test1,test2,test3}/*for f in ./*/*

    【讨论】:

      【解决方案2】:

      你可以给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}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-26
        • 2022-07-11
        • 1970-01-01
        • 2012-08-04
        • 1970-01-01
        • 1970-01-01
        • 2018-10-21
        • 2015-02-18
        相关资源
        最近更新 更多