2011-09-01 67 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

相关问题