中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

每天一個linux命令(21):find命令之xargs

在使(shi)用(yong) find命(ming)(ming)令(ling)的(de)-exec選項處理(li)匹配到(dao)的(de)文件(jian)時, find命(ming)(ming)令(ling)將所有匹配到(dao)的(de)文件(jian)一(yi)起傳(chuan)遞給exec執行。但有些(xie)系統對能(neng)夠傳(chuan)遞給exec的(de)命(ming)(ming)令(ling)長度(du)有限制(zhi),這(zhe)樣在find命(ming)(ming)令(ling)運行幾分鐘之后(hou),就會出現溢(yi)出錯(cuo)誤(wu)。錯(cuo)誤(wu)信(xin)息(xi)通常是(shi)“參數列太長”或“參數列溢(yi)出”。這(zhe)就是(shi)xargs命(ming)(ming)令(ling)的(de)用(yong)處所在,特別是(shi)與find命(ming)(ming)令(ling)一(yi)起使(shi)用(yong)。  

find命(ming)(ming)令把匹(pi)配到的(de)文件傳遞給xargs命(ming)(ming)令,而xargs命(ming)(ming)令每次只(zhi)獲(huo)取一部分文件而不是全部,不像-exec選項(xiang)那樣(yang)。這樣(yang)它可以先處理最(zui)先獲(huo)取的(de)一部分文件,然后是下(xia)一批,并如(ru)此繼續下(xia)去。  

在有些系(xi)統(tong)(tong)(tong)中,使用-exec選項會為處理每(mei)一(yi)(yi)個(ge)匹(pi)配(pei)到(dao)的(de)文(wen)件而(er)發(fa)起一(yi)(yi)個(ge)相(xiang)應的(de)進程(cheng),并非將匹(pi)配(pei)到(dao)的(de)文(wen)件全部作為參數(shu)(shu)(shu)一(yi)(yi)次(ci)執行;這樣在有些情(qing)況(kuang)下就會出現進程(cheng)過(guo)多(duo),系(xi)統(tong)(tong)(tong)性能下降的(de)問題,因而(er)效(xiao)率不高; 而(er)使用xargs命(ming)令(ling)則(ze)只有一(yi)(yi)個(ge)進程(cheng)。另外,在使用xargs命(ming)令(ling)時,究竟(jing)是(shi)(shi)一(yi)(yi)次(ci)獲取所有的(de)參數(shu)(shu)(shu),還是(shi)(shi)分批取得參數(shu)(shu)(shu),以及(ji)每(mei)一(yi)(yi)次(ci)獲取參數(shu)(shu)(shu)的(de)數(shu)(shu)(shu)目都會根據該命(ming)令(ling)的(de)選項及(ji)系(xi)統(tong)(tong)(tong)內核中相(xiang)應的(de)可(ke)調參數(shu)(shu)(shu)來(lai)確定。

使用(yong)實(shi)例(li):

實例1: 查找系(xi)統中(zhong)的(de)每(mei)一個普通文件(jian),然后使(shi)用xargs命令來測試它們(men)分別屬于哪類文件(jian) 

命令:

find . -type f -print | xargs file

輸出:

[root@localhost test]# ll

總計(ji) 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -type f -print | xargs file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ASCII text

[root@localhost test]#

實例(li)2:在整個系(xi)統中查(cha)找內存(cun)信息轉儲文件(core dump) ,然后把結(jie)果保存(cun)到/tmp/core.log 文件中

命令:

 find / -name "core" -print | xargs echo "" >/tmp/core.log

輸出:

[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

[root@localhost test]# cd /tmp

[root@localhost tmp]# ll

總(zong)計 16

-rw-r--r-- 1 root root 1524 11-12 22:29 core.log

drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766

drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815

drwx------ 2 root root 4096 11-03 07:11 vmware-root

實例3:在當(dang)前目(mu)錄下查找所有用(yong)戶具有讀(du)、寫和執行權(quan)限的文(wen)件,并(bing)收(shou)回相應的寫權(quan)限

命令:

find . -perm -7 -print | xargs chmod o-w

輸出(chu):

[root@localhost test]# ll

總計(ji) 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -perm -7 -print | xargs chmod o-w

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 ;root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 ;root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root ;root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 19:32 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

說明:

執行命令(ling)后,文件夾scf、test3和test4的權限都發生(sheng)改(gai)變

實例4:用grep命(ming)令在所有(you)的普通(tong)文件中搜索(suo)hostname這個(ge)詞(ci)

命令:

find . -type f -print | xargs grep "hostname"

輸(shu)出:

[root@localhost test]# find . -type f -print ;| xargs grep "hostname"

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

實例5:用grep命令在當前目錄下(xia)的(de)所有普通(tong)文(wen)件中搜索hostnames這個詞

命令

find . -name \* -type f -print | xargs grep "hostnames"

輸出:

[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

說明:

注意,在(zai)上面的例(li)子(zi)中(zhong)(zhong), \用來取(qu)消find命令(ling)中(zhong)(zhong)的*在(zai)shell中(zhong)(zhong)的特殊含義。  

實例6:使用xargs執行(xing)mv 

命(ming)令:

find . -name "*.log" | xargs ;-i mv {} test4

輸出:

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:54 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

總計 0[root@localhost test4]# cd ..

[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4

[root@localhost test]# ll

總(zong)計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 ;11-13 05:50 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

總(zong)計(ji) 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]#

實例7:find后(hou)執行(xing)xargs提示xargs: argument line too long解(jie)決方法(fa):

命(ming)令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

輸出:

[root@pd test4]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

rm -f 

[root@pdtest4]#

說明:

-l1是(shi)一次處(chu)理(li)一個-t是處(chu)理(li)之前打(da)印出命令

 

實(shi)例8:使(shi)用(yong)-i參(can)數默(mo)認的前(qian)面輸出用(yong){}代替(ti),-I參(can)數可以指(zhi)定其(qi)他代替(ti)字(zi)符(fu),如例子中(zhong)的[] 

命(ming)令(ling):

輸出:

[root@localhost test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 ;test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4

[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..

[root@localhost test4]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 ;root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      ;0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 05:50 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說(shuo)明:

使用-i參(can)數默認(ren)的前面(mian)輸出用{}代替(ti),-I參(can)數可以(yi)指定其他(ta)代替(ti)字符,如例子(zi)中的[] 

實例9:xargs的(de)-p參數(shu)的(de)使用 

命令:

find . -name "*.log" | xargs -p -i mv {} ..

輸出:

[root@localhost test3]# ll

總(zong)計 0

-rw-r--r-- 1 root root ;0 11-13 06:06 log2015.log

[root@localhost test3]# cd ..

[root@localhost test]# ll

總(zong)計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:06 ;test3

drwxrwxr-x 2 root root   4096 ;11-13 05:50 test4

[root@localhost test]# cd test3

[root@localhost test3]#  find . -name "*.log" | xargs -p -i mv {} ..

mv ./log2015.log .. ?...y

[root@localhost test3]# ll

總計 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root  ;   61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      ;0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root  ; 4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說明(ming):

-p參數會提示讓(rang)你確認是(shi)否(fou)執(zhi)行(xing)后面的(de)命令,y執(zhi)行(xing),n不執(zhi)行(xing)。

posted @ 2012-11-15 07:27  peida  閱讀(90402)  評論(4)    收藏  舉報