shell xargs

xargs命令是在管道操作符之后,并通过提供命令行参数执行其他命令。
 
1、多行变成单行
-bash-3.2# cat test.txt 
a b c d e f
g o p q
-bash-3.2# cat test.txt |xargs
a b c d e f g o p q
 
 
2、单行变成多行
-bash-3.2# cat test.txt 
a b c d e f g o p q
-bash-3.2# cat test.txt |xargs -n 2
a b
c d
e f
g o
p q
 
 
3、删除某个重复的字符来做定界符
-bash-3.2# cat test.txt 
aaaagttttgyyyygcccc
-bash-3.2# cat test.txt |xargs -d g
aaaa tttt yyyy cccc
 
 
4、删除某个重复的字符来做定界符后,变成多行
-bash-3.2# cat test.txt |xargs -d g -n 2
aaaa tttt
yyyy cccc
 
 
5、用find找出文件以txt后缀,并使用xargs将这些文件删除
-bash-3.2# find /root/ -name "*.txt" -print        #查找
/root/2.txt
/root/1.txt
/root/3.txt
/root/4.txt
-bash-3.2# find /root/ -name "*.txt" -print0 |xargs -0 rm -rf   #查找并删除
-bash-3.2# find /root/ -name "*.txt" -print          #再次查找没有
 
 
6、查找普通文件中包括thxy这个单词的
-bash-3.2# find /root/ -type f -print |xargs grep "thxy"
/root/1.doc:thxy
 
 
7、查找权限为644的文件,并使用xargs给所有加上x权限
-bash-3.2# find /root/ -perm 644 -print
/root/1.c
/root/5.c
/root/2.doc
/root/3.doc
/root/1.doc
/root/2.c
/root/4.doc
/root/4.c
/root/3.c
-bash-3.2# find /root/ -perm 644 -print|xargs chmod a+x
-bash-3.2# find /root/ -perm 755 -print
/root/1.c
/root/5.c
/root/2.doc
/root/3.doc
/root/1.doc
/root/2.c
/root/4.doc
/root/4.c
/root/3.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值