为什么使用的输出有差异
find . -exec ls '{}' \+
and
find . -exec ls '{}' \;
我有:
$ find . -exec ls \{\} \+
./file1 ./file2
.:
file1 file2 testdir1
./testdir1:
testdir2
./testdir1/testdir2:
$ find . -exec ls \{\} \;
file1 file2 testdir1
testdir2
./file2
./file1
之间的差别;(分号)或+(加号)是参数传入find的-exec/-execdir参数的方式。例如:
using ; will execute multiple commands (separately for each argument),
Example:
$ find /etc/rc* -exec echo Arg: {} ';'
Arg: /etc/rc.common
Arg: /etc/rc.common~previous
Arg: /etc/rc.local
Arg: /etc/rc.netboot
All following arguments to find are taken to be arguments to the command.
The string {} is replaced by the current file name being processed.
using + will execute the least possible commands (as the arguments are combined together). It's very similar to how xargs command works, so it will use as many arguments per command as possible to avoid exceeding the maximum limit of arguments per line.
Example:
$ find /etc/rc* -exec echo Arg: {} '+'
Arg: /etc/rc.common /etc/rc.common~previous /etc/rc.local /etc/rc.netboot
The command line is built by appending each selected file name at the end.
Only one instance of {} is allowed within the command.
参见:
人找到
在find at SO中使用分号(;)vs +(+)和exec
简单的unix命令,{}和\;对于在SO
在find的-exec命令中{}+是什么意思?在Unix
这可以用一个例子来最好地说明。让我们说find会找到这些文件:
file1
file2
file3
使用-exec和分号(find。-exec ls '{}' \;),将执行
ls file1
ls file2
ls file3
但如果你用加号代替(找到。-exec ls '{}' \+),将尽可能多的文件名作为参数传递给单个命令:
ls file1 file2 file3
文件名的数量仅受系统最大命令行长度的限制。如果命令超过这个长度,该命令将被多次调用。
到目前为止所有的答案都是正确的。我提供了一个更清晰的(对我来说)使用echo而不是ls描述行为的演示:
使用分号时,命令echo会在找到每个文件(或其他文件系统对象)时被调用:
$ find . -name 'test*' -exec echo {} \;
./test.c
./test.cpp
./test.new
./test.php
./test.py
./test.sh
使用加号时,命令echo只被调用一次。找到的每个文件都作为参数传入。
$ find . -name 'test*' -exec echo {} \+
./test.c ./test.cpp ./test.new ./test.php ./test.py ./test.sh
如果find显示大量结果,您可能会发现所调用的命令因参数的数量而阻塞。
之间的差别;(分号)或+(加号)是参数传入find的-exec/-execdir参数的方式。例如:
using ; will execute multiple commands (separately for each argument),
Example:
$ find /etc/rc* -exec echo Arg: {} ';'
Arg: /etc/rc.common
Arg: /etc/rc.common~previous
Arg: /etc/rc.local
Arg: /etc/rc.netboot
All following arguments to find are taken to be arguments to the command.
The string {} is replaced by the current file name being processed.
using + will execute the least possible commands (as the arguments are combined together). It's very similar to how xargs command works, so it will use as many arguments per command as possible to avoid exceeding the maximum limit of arguments per line.
Example:
$ find /etc/rc* -exec echo Arg: {} '+'
Arg: /etc/rc.common /etc/rc.common~previous /etc/rc.local /etc/rc.netboot
The command line is built by appending each selected file name at the end.
Only one instance of {} is allowed within the command.
参见:
人找到
在find at SO中使用分号(;)vs +(+)和exec
简单的unix命令,{}和\;对于在SO
在find的-exec命令中{}+是什么意思?在Unix