目录
实现效果:
对前一命令的每一个结果都执行某个操作
参数说明
--exec参数
-exec command:command
-exec 后面接其他执行的指令来处理前一个命令得到的结果。
例如:find /root -name tom_renam -exec grep -nR "hello" {} \;
find在/root目录下搜索名为tom_renam的文件,在搜出的tom_renam文件中搜索“hello”
扫描二维码关注公众号,回复:
13771201 查看本文章

find /root -name tom_renam -exec grep -nR "hello" {} \;
{ } 代表的是「由 find 找到的内容」找到的结果会被放置到 { } 位置中;
-exec 到 ; 代表处理搜寻到的结果的动作,因为「;」在bash的环境下是有特殊意义的,要在前面加反斜杠\转译。
其他例子:
find /root/.ssh -exec ls -l {} ; #显示find /root/.ssh搜出的结果
find /root -size 10M -exec rm -rf {} ; #删除find /root -size 10M搜出的结果
find /root -name tom -exec mv {} tom_rename ; #重命名
find /root -name tom_rename -exec mv {} /tmp ; #将find /root -name tom_rename搜出的移到/tmp
https://www.cnblogs.com/itxdm/p/5936907.html
xargs参数
xargs可以将标准输出(数据流)转换成命令参数作为标准输入( 进行横排输出)。
当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次:
当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次:
#显示。-I {} :定义前一命令的结果放入{}, ls -ld {} : ls -ld 结果
find /root -name Tom | xargs -I {} ls -ld {}
#删除。如果后一条命令的参数在末尾
find /root -name Tom | xargs rm -rf
#移动。如果后一条命令的参数在中间,需指定{},并在后一命令参数的位置放{}占位
find /root -name *.sh| xargs -I {} mv {} /tmp
定义界定符:
在 “xargs” 中是有 “界定符” 的,类似 find 中 { } ,代表的是「由 xargs 找到的内容」。xargs 不需要 “;” 做结束符。
使用 xargs 命令时并不是一定要使用 “{}” 方括号的,可能是因为 find 命令的( -exec )默认是 “{}” (为了统一)使用其他的定义符都是可以的(甚至你都可以用英文,数学等作为定义界定符)
Linux xargs 命令;https://www.runoob.com/linux/linux-comm-xargs.html