你的位置:首页 > 网海拾贝

xargs命令小技巧

2016-03-16 浏览:(7404) 网海拾贝 评论(0)

xargs命令是在管道操作符之后,并通过提供命令行参数执行其他命令。

1、多行变成单行

[root@server1 ~]# cat test.txt
a b c d e f
g o p q
[root@server1 ~]# cat test.txt | xargs
a b c d e f g o p q

2、单行变成多行

[root@server1 ~]# cat test.txt
a b c d e f g o p q
[root@server1 ~]# cat test.txt | xargs -n 2
a b
c d
e f
g o
p q

3、删除某个重复的字符来做定界符

[root@server1 ~]# cat test.txt
aaaagttttgyyyygcccc
[root@server1 ~]# cat test.txt | xargs -d g
aaaa tttt yyyy cccc

4、删除某个重复的字符来做定界符后,变成多行

[root@server1 ~]# cat test.txt | xargs -d g -n 2
aaaa tttt
yyyy cccc

5、用find找出文件以txt后缀,并使用xargs将这些文件删除

[root@server1 ~]# find /root/ -name "*.txt" -print        #查找
/root/2.txt
/root/1.txt
/root/3.txt
/root/4.txt
[root@server1 ~]# find /root/ -name "*.txt" -print0 |xargs -0 rm -rf   #查找并删除

6、查找普通文件中包括thxy这个单词的

[root@server1 ~]# find /root/ -type f -print |xargs grep "thxy"
/root/1.doc:thxy

7、查找权限为644的文件,并使用xargs给所有加上x权限

[root@server1 ~]# 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
[root@server1 ~]# find /root/ -perm 644 -print|xargs chmod a+x
[root@server1 ~]# 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

  • 发表评论
  • 查看评论
【暂无评论!】

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。