|
发表于 2006-5-15 19:46:53
|
显示全部楼层
find . -name "core" -print | xargs >/tmp/core.log
和
find . -name "core" | xargs echo >/tmp/core.log
是一样的,更简明的是
find . -name "core" | xargs >/tmp/core.log
因为find的默认命令是print,xargs的默认命令是/bin/echo。
但和
find . -name "core" -print | xargs echo "" >/tmp/core.log
有区别。
和
find . -name "core" -exec echo {} \; >/tmp/core.log
更不一样,你可以看到在这里find pipe到xargs后输出的结果是文件名以空格分隔的一行,大概作者要的就是把这个效果。但我不清楚echo ""在行首加上一个空格是何意,另外文件名中有空格的话,那么这个`以空隔分隔的行'就没什么意思了,处理起来反而麻烦了。
xargs默认的分隔符是空隔和newline,所以文件名中如果有空格或者newline的话对于xargs就有麻烦了,一般来说
find print0 | xargs -0的配合才是绝对可靠的。但在这里,最终的输出没什么区别。
-exec和xargs的主要区别在于效率,find -exec command,每次对文件的处理重建一个command的进程,而pipe到xargs则一般情况下command只要执行一次,也就是一次性把从标准输出来的文件名送给command处理。
至于`\;',`\'是转义符,防止shell抢先对`;'解释,';'单引号的效果是一样的
- -exec command ;
- 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.
复制代码
还是直接引用man吧,我觉得自己有些辞不达意了。 |
|