For loop for files in multiple folders - bash shell

后端 未结 2 743
失恋的感觉
失恋的感觉 2021-01-02 09:43

I need to have files from many directories in a for loop. As for now, I have the following code:

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


        
2条回答
  •  醉梦人生
    2021-01-02 10:00

    You can give multiple "words" to for, so the simplest answer is:

    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}
    

提交回复
热议问题