2011-05-25 69 views
6

我需要在for循環中有許多目錄中的文件。 至於現在,我有以下代碼:for循環多個文件夾中的文件 - bash shell

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

在每一個循環,我做同樣的事情。有沒有辦法從多個文件夾中獲取文件?

在此先感謝

回答

12

嘗試取決於你想要什麼for f in ./{test1,test2,test3}/*for f in ./*/*

+0

謝謝,這對我有用! – fanjabi 2011-05-25 09:46:59

+0

我的榮幸!你能將這個答案標記爲「接受」嗎?乾杯。 – evgeny 2011-05-25 09:55:23

5

你可以給多個「詞」來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}