For a loop for files in multiple folders - bash shell

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

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

In each cycle, I do the same. Is there a way to get files from multiple folders?

Thanks in advance

+6
source share
2 answers

Try for f in ./{test1,test2,test3}/* or for f in ./*/* depending on what you want.

+12
source

You can give a few " for " words, so the simplest answer is:

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

There are various tricks to reduce the amount of input; namely: extension and brackets.

 # 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} 
+5
source

Source: https://habr.com/ru/post/888991/


All Articles