每天一個linux命令(21):find命令之xargs 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html;每天一個linux命令(5):rm命令http://www.bkjia.com/os/201210/163662.html;每天一個linux命令(6):rmdir命令http://www.bkjia.com/os/201210/164017.html;每天一個linux命令(7):mv命令http://www.bkjia.com/os/201210/164247.html;每天一個linux命令(8):cp命令http://www.bkjia.com/os/201210/164254.html;每天一個linux命令(9):touch命令http://www.bkjia.com/os/201211/165699.html;每天一個linux命令(10):cat命令http://www.bkjia.com/os/201211/165989.html;每天一個linux命令(11):nl命令http://www.bkjia.com/os/201211/165990.html每天一個linux命令(12):more命令http://www.bkjia.com/os/201211/165994.html每天一個linux命令(13):less命令http://www.bkjia.com/os/201211/165998.html每天一個linux命令(14):head命令http://www.bkjia.com/os/201211/166191.html每天一個linux命令(15):tail命令http://www.bkjia.com/os/201211/168702.html每天一個linux命令(16):which命令http://www.bkjia.com/os/201211/168890.html每天一個linux命令(17):whereis命令http://www.bkjia.com/os/201211/168893.html每天一個linux命令(18):locate命令http://www.bkjia.com/os/201211/168895.html每天一個linux命令(19):find命令概覽http://www.bkjia.com/os/201211/168897.html每天一個linux命令(20):find命令之exechttp://www.bkjia.com/os/201211/168901.html 在使用 find命令的-exec選項處理匹配到的檔案時, find命令將所有匹配到的檔案一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之後,就會出現溢出錯誤。錯誤資訊通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。 www.2cto.com find命令把匹配到的檔案傳遞給xargs命令,而xargs命令每次只擷取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先擷取的一部分檔案,然後是下一批,並如此繼續下去。 在有些系統中,使用-exec選項會為處理每一個匹配到的檔案而發起一個相應的進程,並非將匹配到的檔案全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統效能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次擷取所有的參數,還是分批取得參數,以及每一次擷取參數的數目都會根據該命令的選項及系統核心中相應的可調參數來確定。使用執行個體:執行個體1: 尋找系統中的每一個普通檔案,然後使用xargs命令來測試它們分別屬於哪類檔案 命令:find . -type f -print | xargs file輸出:[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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 11-12 19:32 test3drwxrwxrwx 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]#執行個體2:在整個系統中尋找記憶體資訊轉儲檔案(core dump) ,然後把結果儲存到/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總計 16-rw-r--r-- 1 root root 1524 11-12 22:29 core.logdrwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815drwx------ 2 root root 4096 11-03 07:11 vmware-root執行個體3:在目前的目錄下尋找所有使用者具有讀、寫和執行許可權的檔案,並收回相應的寫入權限命令: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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 11-12 19:32 test3drwxrwxrwx 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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-12 19:32 test3drwxrwxr-x 2 root root 4096 11-12 19:32 test4[root@localhost test]#說明:執行命令後,檔案夾scf、test3和test4的許可權都發生改變執行個體4:用grep命令在所有的普通檔案中搜尋hostname這個詞命令:find . -type f -print | xargs grep "hostname"輸出:[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命令在目前的目錄下的所有普通檔案中搜尋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]#說明:注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。 執行個體6:使用xargs執行mv 命令: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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-12 22:54 test3drwxrwxr-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總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 05:50 test3drwxrwxr-x 2 root root 4096 11-13 05:50 test4[root@localhost test]# cd test4/[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]#執行個體7:find後執行xargs提示xargs: argument line too long解決方案:命令: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 -frm -f [root@pdtest4]#說明:-l1是一次處理一個;-t是處理之前列印出命令 執行個體8:使用-i參數預設的前面輸出用{}代替,-I參數可以指定其他代替字元,如例子中的[] 命令:輸出:[root@localhost test]# ll總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 05:50 test3drwxrwxr-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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 05:50 test3drwxrwxr-x 2 root root 4096 11-13 05:50 test4[root@localhost test]#說明:使用-i參數預設的前面輸出用{}代替,-I參數可以指定其他代替字元,如例子中的[] 執行個體9:xargs的-p參數的使用 命令:find . -name "*.log" | xargs -p -i mv {} ..輸出:[root@localhost test3]# ll總計 0-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log[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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 06:06 test3drwxrwxr-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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-x 2 root root 4096 11-13 05:50 test4[root@localhost test]#說明:-p參數會提示讓你確認是否執行後面的命令,y執行,n不執行。