標(biāo)簽: 雜談 | 分類: LINUX學(xué)習(xí)日記 |
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、刪除某個(gè)重復(fù)的字符來(lái)做定界符
-bash-3.2# cat test.txt
aaaagttttgyyyygcccc
-bash-3.2# cat test.txt |xargs -d g
aaaa tttt yyyy cccc
4、刪除某個(gè)重復(fù)的字符來(lái)做定界符后,變成多行
-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這個(gè)單詞的
-bash-3.2# find /root/ -type f -print |xargs grep "thxy"
/root/1.doc:thxy
7、查找權(quán)限為644的文件,并使用xargs給所有加上x(chóng)權(quán)限
-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
8、ps -ef|grep LOCAL=NO|grep -vgrep|cut -c 9-15|xargs kill -9
運(yùn)行這條命令將會(huì)殺掉所有含有關(guān)鍵字"LOCAL=NO"的進(jìn)程:
管道符"|"用來(lái)隔開(kāi)兩個(gè)命令,管道符左邊命令的輸出會(huì)作為管道符右邊命令的輸入。
"ps -ef"是linux里查看所有進(jìn)程的命令。這時(shí)檢索出的進(jìn)程將作為下一條命令"grep LOCAL=NO"的輸入。
"grepLOCAL=NO" 的輸出結(jié)果是,所有含有關(guān)鍵字"LOCAL=NO"的進(jìn)程。
"grep -vgrep" 是在列出的進(jìn)程中去除含有關(guān)鍵字"grep"的進(jìn)程。
"cut -c9-15" 是截取輸入行的第9個(gè)字符到第15個(gè)字符,而這正好是進(jìn)程號(hào)PID。
"xargs kill-9" 中的 xargs 命令是用來(lái)把前面命令的輸出結(jié)果(PID)作為"kill-9"命令的參數(shù),并執(zhí)行該命令。"kill -9"會(huì)強(qiáng)行殺掉指定進(jìn)程。
其它類似的情況,只需要修改"grep LOCAL=NO"中的關(guān)鍵字部分就可以了。
另一種方法,使用awk
ps x|grep gas|grep -v grep |awk '{print$1}'|xargs kill -9
另:
xargs 與find 命令合用的時(shí)候,find 把匹配到得命令傳遞給xargs ,xargs每次只獲取一部分文件,而不是全部。分批處理。
xargs則只有一個(gè)進(jìn)程、但xargs 處理是否分批 ,批次大小,也會(huì)受系統(tǒng)些可調(diào)參數(shù)影響。
來(lái)自:http://blog.csdn.net/yasi_xi/article/details/8637069
聯(lián)系客服