使用 xargs 将查找结果中的目录 mv 到另一个目录中

人气:311 发布:2022-10-16 标签: shell find xargs mv

问题描述

我有以下命令:

find . -type d -mtime 0 -exec mv {} /path/to/target-dir ;

这会将创建的目录移动到另一个目录.我怎样才能使用 xargs 而不是 exec 来做同样的事情.

This will move the directory founded to another directory. How can I use xargs instead of exec to do the same thing.

推荐答案

如果你有 GNU mv(以及findxargs),可以使用-t选项到 mv(和 -print0 用于 find-0 用于 xargs):

If you've got GNU mv (and find and xargs), you can use the -t option to mv (and -print0 for find and -0 for xargs):

find . -type d -mtime -0 -print0 | xargs -0 mv -t /path/to/target-dir

请注意,现代版本的 find(与 POSIX 2008 兼容)支持 + 代替 ; 并且行为与 xargs 不使用 xargs:

Note that modern versions of find (compatible with POSIX 2008) support + in place of ; and behave roughly the same as xargs without using xargs:

find . -type d -mtime -0 -exec mv -t /path/to/target-dir {} +

这使得 find 将方便数量的文件(目录)名称组合到程序的单个调用中.您无法控制 xargs 提供的传递给 mv 的参数数量,但您实际上很少需要它.这仍然取决于 GNU mv-t 选项.

This makes find group convenient numbers of file (directory) names into a single invocation of the program. You don't have the level of control over the numbers of arguments passed to mv that xargs provides, but you seldom actually need that anyway. This still hinges on the -t option to GNU mv.

999