如何将目录中仅为普通文件(忽略子目录和链接)的文件复制到相同目标?(在Linux上使用bash) 有大量的文件
如何将目录中仅为普通文件(忽略子目录和链接)的文件复制到相同目标?(在Linux上使用bash) 有大量的文件
for file in /source/directory/*
do
if [[ -f $file ]]; then
#copy stuff ....
fi
done
ARG_MAX
最大命令行长度限制的影响,因为没有涉及到外部实用程序。话虽如此,在Bash中具有大量迭代次数的任何循环都会变得_缓慢_. - mklement0[[ ... ]]
而不是[ ... ]
,因此可以正确处理带有空格的文件名;在大多数情况下,[[ ... ]]
不需要对变量引用进行双引号引用。 - mklement0列出 /my/sourcedir/
中的常规文件,不递归查找子目录:
find /my/sourcedir/ -type f -maxdepth 1
将这些文件复制到 /my/destination/
:
find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \;
find /data/* -maxdepth 2 -type f
- Anum Sheraz为了进一步说明 poplitea的回答,你不必为每个文件执行cp命令:可以使用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 '{}' +
cp -t
是 GNU 扩展。 - mklement0