2011-09-01 89 views

回答

36
for file in /source/directory/* 
do 
    if [[ -f $file ]]; then 
     #copy stuff .... 
    fi 
done 
+6

非常大量的文件,由於shell擴展限制這將無法正常工作 – holygeek

+0

它也不會尊重與空白文件在名字裏。 –

+0

@holygeek:不,globbing(路徑名擴展)不受'ARG_MAX'最大限制。命令行長度限制,因爲不涉及外部實用程序。也就是說,任何在Bash中有大量迭代的循環都是_slow_。 – mklement0

20

的要列出常規文件中/my/sourcedir/,在子目錄不遞歸尋找:

find /my/sourcedir/ -type f -maxdepth 1 

將這些文件複製到/my/destination/

find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \; 
7

要擴大poplitea's answer,您不必爲每個文件執行cp:u SE xargs多個文件一次複製:

find /my/sourcedir -maxdepth 1 -type f -print0 | xargs -0 cp -t /my/destination 

find /my/sourcedir -maxdepth 1 -type f -exec cp -t /my/destination '{}' + 
+0

'-exec ... +'是最有效的解決方案;值得一提的是:'cp -t'是一個_GNU_擴展名。 – mklement0

相關問題