如何递归复制所有不超过 1 天的文件?

Ort*_*kni 2 find xargs cp

如何递归复制所有不超过 1 天的文件?

我第一次尝试

find . -amin -1440 | xargs cp /dest
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为给予的项目xargs应该在/dest参数之前定位。

ter*_*don 7

您可以使用-tGNU 选项cp

   -t, --target-directory=DIRECTORY
          copy all SOURCE arguments into DIRECTORY
Run Code Online (Sandbox Code Playgroud)

您还应该使用find' -print0xargs -0否则,这将在带有空格或其他奇怪字符的文件名上失败:

find . -amin -1440 -print0 | xargs -O cp -t /dest
Run Code Online (Sandbox Code Playgroud)

更好的方法可能是使用find自身并xargs完全避免:

find . -amin -1440 -exec cp -t /dest {} +
Run Code Online (Sandbox Code Playgroud)

最后,既然你提到了“文件”,你可能想跳过目录、符号链接和其他奇怪的东西,并告诉find只查找常规文件:

find . -type f -amin -1440 -exec cp -t /dest {} +
Run Code Online (Sandbox Code Playgroud)