我想知道到底是什么{} \;和{} \+和| xargs ...做的.请用解释澄清这些.
以下3个命令运行并输出相同的结果,但第一个命令需要一点时间,格式也略有不同.
find . -type f -exec file {} \;
find . -type f -exec file {} \+
find . -type f | xargs file
Run Code Online (Sandbox Code Playgroud)
这是因为第一个file为来自命令的每个文件运行find命令.所以,基本上它运行如下:
file file1.txt
file file2.txt
Run Code Online (Sandbox Code Playgroud)
但后两个-exec命令运行文件命令一次为所有文件,如下所示:
file file1.txt file2.txt
Run Code Online (Sandbox Code Playgroud)
然后我运行以下命令,第一个运行没有问题,但第二个给出错误消息.
find . -type f -iname '*.cpp' -exec mv {} ./test/ \;
find . -type f -iname '*.cpp' -exec mv {} ./test/ \+ #gives error:find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)
对于命令 …