歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> xargs命令

xargs命令

日期:2017/2/27 16:04:04   编辑:Linux教程
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
Copyright © Linux教程網 All Rights Reserved