为什么find -exec mv {} ./target/+不起作用? [英] Why does find -exec mv {} ./target/ + not work?

查看:107
本文介绍了为什么find -exec mv {} ./target/+不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确切地知道{} \;{} \+| xargs ...的作用.请通过解释加以澄清.

I want to know exactly what {} \; and {} \+ and | xargs ... do. Please clarify these with explanations.

下面的3条命令运行并输出相同的结果,但是第一个命令花费一些时间,格式也几乎没有什么不同.

Below 3 commands run and output same result but the first command takes a little time and the format is also little different.

find . -type f -exec file {} \;
find . -type f -exec file {} \+
find . -type f | xargs file

这是因为第一个文件对每个来自find命令的文件运行file命令.因此,基本上它的运行方式为:

It's because 1st one runs the file command for every file coming from the find command. So, basically it runs as:

file file1.txt
file file2.txt

但是后2个使用-exec命令查找的文件对所有文件都运行一次,如下所示:

But latter 2 find with -exec commands run file command once for all files like below:

file file1.txt file2.txt

然后我运行以下命令,第一个在没有问题的情况下运行,但是第二个给出错误消息.

Then I run the following commands on which first one runs without problem but second one gives error message.

find . -type f -iname '*.cpp' -exec mv {} ./test/ \;
find . -type f -iname '*.cpp' -exec mv {} ./test/ \+ #gives error:find: missing argument to `-exec'

对于使用{} \+的命令,它会给我错误消息

For command with {} \+, it gives me the error message

find: missing argument to `-exec'

那是为什么?谁能解释我在做什么错?

why is that? can anyone please explain what am I doing wrong?

推荐答案

手册页(或在线GNU手册)几乎说明了一切.

The manual page (or the online GNU manual) pretty much explains everything.

对于每个结果,执行command {}. {}的所有出现都由文件名替换. ;带有斜杠前缀以防止外壳解释它.

For each result, command {} is executed. All occurences of {} are replaced by the filename. ; is prefixed with a slash to prevent the shell from interpreting it.

每个结果都附加到command并随后执行.考虑到命令长度的限制,我猜想该命令可能会执行更多次,而手册页会为我提供支持:

Each result is appended to command and executed afterwards. Taking the command length limitations into account, I guess that this command may be executed more times, with the manual page supporting me:

该命令的调用总数将大大少于匹配文件的数目.

the total number of invocations of the command will be much less than the number of matched files.

请注意手册页中的这句话:

Note this quote from the manual page:

构建命令行的方式与xargs构建命令行的方式基本相同

The command line is built in much the same way that xargs builds its command lines

这就是为什么{}+之间除空白外不允许任何字符的原因. +使find检测到应像xargs一样将参数附加到命令中.

That's why no characters are allowed between {} and + except for whitespace. + makes find detect that the arguments should be appended to the command just like xargs.

幸运的是,mv的GNU实现可以使用-t或更长的参数--target接受目标目录作为参数.用法是:

Luckily, the GNU implementation of mv can accept the target directory as an argument, with either -t or the longer parameter --target. It's usage will be:

mv -t target file1 file2 ...

您的find命令变为:

find . -type f -iname '*.cpp' -exec mv -t ./test/ {} \+


在手册页上:


From the manual page:

-exec命令;

-exec command ;

执行命令;如果返回0状态,则为true.以下所有要查找的参数都将被视为命令的参数,直到由';'组成的参数为止遇到.字符串"{}"被当前文件名替换,该文件名在命令的参数中出现的所有位置(不仅仅是在单独的参数中,例如在某些版本的find中)都会被处理.可能需要对这两种构造进行转义(带有"\")或加引号,以防止它们被外壳扩展.有关使用-exec选项的示例,请参见示例部分.对于每个匹配的文件,指定的命令运行一次.该命令在起始目录中执行.与-exec操作的使用有关的不可避免的安全问题;您应该改用-execdir选项.

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

-exec命令{} +

-exec command {} +

-exec操作的此变体在选定的文件上运行指定的命令,但是通过在末尾附加每个选定的文件名来构建命令行.该命令的调用总数将远远少于匹配文件的数目.命令行的构建与xargs构建其命令行的方式几乎相同.命令中仅允许一个{{}"的实例.该命令在起始目录中执行.

This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is allowed within the command. The command is executed in the starting directory.

这篇关于为什么find -exec mv {} ./target/+不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆