1. 程式人生 > >linux每日命令(23):find命令之xargs

linux每日命令(23):find命令之xargs

在使用 find命令的-exec選項處理匹配到的檔案時, find命令將所有匹配到的檔案一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令執行幾分鐘之後,就會出現溢位錯誤。錯誤資訊通常是“引數列太長”或“引數列溢位”。這就是xargs命令的用處所在,特別是與find命令一起使用。

find命令把匹配到的檔案傳遞給xargs命令,而xargs命令每次只獲取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分檔案,然後是下一批,並如此繼續下去。

在有些系統中,使用-exec選項會為處理每一個匹配到的檔案而發起一個相應的程序,並非將匹配到的檔案全部作為引數一次執行;這樣在有些情況下就會出現程序過多,系統性能下降的問題,因而效率不高; 而使用xargs命令則只有一個程序。另外,在使用xargs命令時,究竟是一次獲取所有的引數,還是分批取得引數,以及每一次獲取引數的數目都會根據該命令的選項及系統核心中相應的可調引數來確定。

1. 從當前目錄下開始查詢的所有檔案,然後使用xargs命令來測試它們分別屬於哪類檔案

命令:

 find . -type f -print |xargs file

輸出:

[[email protected] test]# ls
dir1  log1  log2
[[email protected] test]# find . -type f -print |xargs file
./log1: empty
./log2: ASCII text

2. 從根目錄/開始查詢名為core的檔案或目錄,並將查詢結果儲存到/tmp/core.log 檔案中

命令:

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

輸出:

[[email protected] tmp]# ls
xmlXPathIniteihlTv.c  xmlXPathInitLrmz_p.c  xmlXPathInitpywFgf.c  xmlXPathInitv76QxM.c  yum_save_tx.2018-11-15.18-23.5nqJ3w.yumtx  yum_save_tx.2018-11-16.23-54.cMoa46.yumtx
[[email protected] tmp]# find / -name 'core' -print  |xargs   > /tmp/core.log
[[email protected]
tmp]# ls core.log xmlXPathIniteihlTv.c xmlXPathInitLrmz_p.c xmlXPathInitpywFgf.c xmlXPathInitv76QxM.c yum_save_tx.2018-11-15.18-23.5nqJ3w.yumtx yum_save_tx.2018-11-16.23-54.cMoa46.yumtx [[email protected] tmp]# cat core.log /dev/core /proc/sys/net/core /usr/lib/python2.7/site-packages/firewall/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/infiniband/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/memstick/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/mmc/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/net/ethernet/mellanox/mlx5/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/usb/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/net/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/sound/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/infiniband/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/memstick/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/mmc/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/net/ethernet/mellanox/mlx5/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/usb/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/net/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/sound/core

說明:

>  
是定向輸出到檔案,如果檔案不存在,就建立檔案;如果檔案存在,就將其清空;一般我們備份清理日誌檔案的時候,就是這種方法:先備份日誌,再用>,將日誌檔案清空(檔案大小變成0位元組);

>> 
這個是將輸出內容追加到目標檔案中。如果檔案不存在,就建立檔案;如果檔案存在,則將新的內容追加到那個檔案的末尾,該檔案中的原有內容不受影響。

3:從當前目錄下開始查詢其他使用者具有讀、寫和執行許可權的檔案,並收回相應的寫許可權

命令:

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

輸出:

[[email protected] test]# ll
total 4
drwxr-xr-x. 2 root root 6 Nov 20 18:28 dir1
-rwxrwxrwx. 1 root root 0 Nov 20 18:28 log1
-rw-r--r--. 1 root root 4 Nov 20 18:29 log2
[[email protected] test]# find . -perm -7 -print | xargs chmod o-w
[[email protected] test]# ll
total 4
drwxr-xr-x. 2 root root 6 Nov 20 18:28 dir1
-rwxrwxr-x. 1 root root 0 Nov 20 18:28 log1
-rw-r--r--. 1 root root 4 Nov 20 18:29 log2

說明:
可以看到,執行命令前 log1檔案,所屬使用者 所屬組 其他使用者均有讀、寫、執行許可權,執行命令後,其他使用者沒有了寫許可權,其他許可權都還在

4. 用grep命令在從當前目錄下開始查詢型別為檔案,且檔案內容中含有hostname的檔案

命令:

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

輸出:

[[email protected] test]# ls
dir1  log1  log2
[[email protected] test]# cat log1
[[email protected] test]# cat log2
我是log2
hostnamesina=sina.com 哈哈
第三行
[[email protected] test]# find . -type f -print | xargs grep "hostname"
./log2:hostnamesina=sina.com 哈哈

說明:
Linux grep命令用於查詢檔案裡符合條件的字串。

grep指令用於查詢內容包含指定的範本樣式的檔案,如果發現某檔案的內容符合所指定的範本樣式,預設grep指令會把含有範本樣式的那一行顯示出來

5. 從當前目錄下開始查詢名稱中以log開頭的檔案或目錄,並將其移動到dir1目錄中

命令:

 find . -name 'log*' | xargs -i mv {} dir1

輸出:

[[email protected] test]# ls
dir1   log1  log2
[[email protected] test]# find . -name 'log*' | xargs -i mv {} dir1
[[email protected] test]# ls
dir1
[[email protected] test]# cd dir1/
[[email protected] dir1]# ls
log1  log2

說明:
{} 花括號代表前面find查找出來的檔名。

6. 從當前目錄下開始查詢名稱中以log開頭的檔案或目錄,並將其移動到當前目錄的父級目錄中,移動時,進行詢問

命令:

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

輸出:

[[email protected] test]# ls
dir1
[[email protected] test]# cd dir1/
[[email protected] dir1]# ls
log1  log2  log3
[[email protected] dir1]# find . -name "log*" | xargs -p -i mv {} ..
mv ./log1 .. ?...y
mv ./log2 .. ?...y
mv ./log3 .. ?...n
[[email protected] dir1]# ls
log3
[[email protected] dir1]# cd ..
[[email protected] test]# ls
dir1  log1  log2

說明:

-p引數會提示讓你確認是否執行後面的命令,y執行,n不執行。

7.find後執行xargs提示xargs: argument line too long解決方法:

命令:

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

輸出:

[[email protected] dir1]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f 
[[email protected]  dir1]#

說明:

-l1  是指一次處理一個
-t   是指處理之前打印出的命令
-print 在每一個輸出後會新增一個回車換行符,而-print0則不會。