find -exec 与xargs 区别
2014-03-20 15:58
239 查看
find . -name "*.txt" -exec rm {} \;
find . -name "*.txt" | xargs rm {}
-exec
1.参数是一个一个传递的,传递一个参数执行一次rm
2.文件名有空格等特殊字符也能处理
-xargs
1.一次将参数传给命令,可以使用-n控制参数个数
2.处理特殊文件名需要采用如下方式:
find . -name "*.txt" print0 |xargs -0 rm {}
实验结果如下,可以清楚看到参数传递过程
[cpp] view
plaincopy
[root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n 2 echo
echo ./data.txt ./env2.txt
./data.txt ./env2.txt
echo ./env.txt ./export2.txt
./env.txt ./export2.txt
echo ./s.txt ./d.txt
./s.txt ./d.txt
echo ./export.txt ./set.txt
./export.txt ./set.txt
echo ./fuck.txt
./fuck.txt
[root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
begin ./data.txt
begin ./env2.txt
begin ./env.txt
begin ./export2.txt
begin ./s.txt
begin ./d.txt
begin ./export.txt
begin ./set.txt
begin ./fuck.txt
[root@andes.com ~/tmp/dir]#
技巧: find -print0 与 xargs -0 的结合避免文件名有特殊字符如空格,引号等无法处理:
find . -name "*.txt" print0 |xargs -0 rm {}
find
-print True; print the full file name on the standard output, followed by a newline. If you are piping the
output of find into another program and there is the faintest possibility that the files which you
are searching for might contain a newline, then you should seriously consider using the -print0
option instead of -print. See the UNUSUAL FILENAMES section for information about how unusual char-
acters in filenames are handled.
-print0
True; print the full file name on the standard output, followed by a null character (instead of the
newline character that -print uses). This allows file names that contain newlines or other types of
white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the -0 option of xargs.
xargs
-0 Input items are terminated by a null character insteadof by
whitespace, and the quotes and backslash are not special (every
character is taken literally). Disables the end of file string,
which istreated likeany other argument. Useful when input
items might contain white space, quote marks, or backslashes.
The GNU
find -print0 option produces input suitable for this
mode.
find . -name "*.txt" | xargs rm {}
-exec
1.参数是一个一个传递的,传递一个参数执行一次rm
2.文件名有空格等特殊字符也能处理
-xargs
1.一次将参数传给命令,可以使用-n控制参数个数
2.处理特殊文件名需要采用如下方式:
find . -name "*.txt" print0 |xargs -0 rm {}
实验结果如下,可以清楚看到参数传递过程
[cpp] view
plaincopy
[root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n 2 echo
echo ./data.txt ./env2.txt
./data.txt ./env2.txt
echo ./env.txt ./export2.txt
./env.txt ./export2.txt
echo ./s.txt ./d.txt
./s.txt ./d.txt
echo ./export.txt ./set.txt
./export.txt ./set.txt
echo ./fuck.txt
./fuck.txt
[root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
begin ./data.txt
begin ./env2.txt
begin ./env.txt
begin ./export2.txt
begin ./s.txt
begin ./d.txt
begin ./export.txt
begin ./set.txt
begin ./fuck.txt
[root@andes.com ~/tmp/dir]#
技巧: find -print0 与 xargs -0 的结合避免文件名有特殊字符如空格,引号等无法处理:
find . -name "*.txt" print0 |xargs -0 rm {}
find
-print True; print the full file name on the standard output, followed by a newline. If you are piping the
output of find into another program and there is the faintest possibility that the files which you
are searching for might contain a newline, then you should seriously consider using the -print0
option instead of -print. See the UNUSUAL FILENAMES section for information about how unusual char-
acters in filenames are handled.
-print0
True; print the full file name on the standard output, followed by a null character (instead of the
newline character that -print uses). This allows file names that contain newlines or other types of
white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the -0 option of xargs.
xargs
-0 Input items are terminated by a null character insteadof by
whitespace, and the quotes and backslash are not special (every
character is taken literally). Disables the end of file string,
which istreated likeany other argument. Useful when input
items might contain white space, quote marks, or backslashes.
The GNU
find -print0 option produces input suitable for this
mode.
相关文章推荐
- 关于 ‘--exec’ 参数( find 命令)及介绍 ‘xargs ’命令区别
- 使用find命令时 |xargs(管道xargs)与-exec的区别
- find -exec 与 xargs的区别与使用
- find -exec 与xargs 区别
- 老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exec有什么区别?
- find -exec 与xargs 区别
- 关于 ‘--exec’ 参数( find 命令)及介绍 ‘xargs ’命令区别(新版)
- find只查当前目录 和 -exec和xargs区别
- find -exec 与xargs 区别
- find 命令-exec 参数与 xargs之间的区别
- find中的-print0和xargs中-0的区别
- linux-shell-find,xargs,exec-查找技巧
- -exec和xargs的区别
- find 与 xargs 、exec
- find命令--xargs--exec
- BASH命令和SHELL脚本总结(15)find和xargs和exec和rsync
- -exec和xargs的区别
- xargs、管道、exec区别
- find,exec和xargs使用
- -exec和xargs的区别