歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(21)find命令_xargs參數

每天一個Linux命令(21)find命令_xargs參數

日期:2017/3/3 12:35:33   编辑:Linux技術

xargs 與 exec 的作用類似,但是xargs與find 一起使用時,一般配合管道一起使用。 前面的輸出轉換為後方指令的參數輸入,使用exec和xargs可以使用戶對所匹配到的文件執行幾乎所有的命令。

[b] (1)用法:[/b][b] 用法: [find命令] | [xargs] [其他命令][/b]

[b] (2)功能:[/b][b] 功能: 該命令的主要功能是從輸入中構建和執行shell命令。與-exec類似,將find找到的文件當作參數執行接下來的命令。[/b]

[b] (3)xargs參數的解釋[/b] 在使用find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之後,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。

find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然後是下一批,並如此繼續下去。 在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。

[b] (4)實例:[/b] 1)[root@localhost Documents]# find . -type f -print     find的輸出到標准輸出的參數-print

[root@localhost Documents]# find . -type f  -print    //等價於find . -type f 因為默認是輸出到標准輸出的,所以加不加標准輸出-print一樣
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
2)[root@localhost Documents]# find . -type f -print | xargs file        查找當前目錄下的每一個普通文件,然後使用xargs命令來測試它們分別屬於哪類文件

[root@localhost Documents]# find . -type f -print | xargs file
./less1:          ASCII text
./less2:          ASCII text
./head_text:      ASCII text
./tail_text:      ASCII text
./tempory:        ASCII text
./newlocate:      empty
./uText:          empty
./findDir/t1.txt: empty
./findDir/T1.txt: empty
./findDir/T2.txt: empty
./findDir/p1.pdf: empty
./findDir/p2.pdf: empty
3)[root@localhost Documents]# find . -type f | xargs ls -l        列出每個查找到的文件的詳細信息,包括相對路徑

[root@localhost Documents]# find . -type f | xargs ll           //為每一個找到的文件執行ll命令顯然是不行的(雖然ll是“ls -l”的縮寫)
xargs: ll: 沒有那個文件或目錄
[root@localhost Documents]# find . -type f | xargs ls -l
-rw-r--r--. 1 root root   0 5月  17 04:18 ./findDir/p1.pdf
-rw-r--r--. 1 root root   0 5月  17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root   0 5月  17 03:50 ./findDir/t1.txt
-rw-r--r--. 1 root root   0 5月  17 04:02 ./findDir/T1.txt
-rw-r--r--. 1 root root   0 5月  17 04:02 ./findDir/T2.txt
-rw-r--r--. 1 root root 664 5月   9 07:59 ./head_text
-rw-r--r--. 1 root root  45 5月   9 08:15 ./less1
-rw-r--r--. 1 root root  57 5月   9 08:16 ./less2
-rw-r--r--. 1 root root   0 5月  15 18:21 ./newlocate
-rw-r--r--. 1 root root 259 5月  12 21:53 ./tail_text
-rw-r--r--. 1 root root 216 5月  12 22:24 ./tempory
-rw-r--r--. 1 root root   0 5月  15 18:34 ./uText
[root@localhost Documents]# ls -l
總用量 20
drwxr-xr-x. 2 root root  71 5月  17 04:18 findDir
-rw-r--r--. 1 root root 664 5月   9 07:59 head_text
-rw-r--r--. 1 root root  45 5月   9 08:15 less1
-rw-r--r--. 1 root root  57 5月   9 08:16 less2
-rw-r--r--. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月  12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月  12 22:24 tempory
-rw-r--r--. 1 root root   0 5月  15 18:34 uText
4)[root@localhost Documents]# find . -type f -print | xargs chmod a-r         回收文件的權限(r代表讀,w代表寫,r代表可執行)

[root@localhost Documents]# find . -type f -print | xargs chmod a-x    //回收x權限
[root@localhost Documents]# ll
總用量 20
drwxr-xr-x. 2 root root  71 5月  17 04:18 findDir
-rw-r--r--. 1 root root 664 5月   9 07:59 head_text
-rw-r--r--. 1 root root  45 5月   9 08:15 less1
-rw-r--r--. 1 root root  57 5月   9 08:16 less2
-rw-r--r--. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月  12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月  12 22:24 tempory
-rw-r--r--. 1 root root   0 5月  15 18:34 uText
[root@localhost Documents]# find . -type f -print | xargs chmod a-r  //回收r權限
[root@localhost Documents]# ll
總用量 20
drwxr-xr-x. 2 root root  71 5月  17 04:18 findDir
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
--w-------. 1 root root   0 5月  15 18:21 newlocate
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
5)[root@localhost sunjimeng]# find ./Document -name "all.txt" -print | xargs echo "Success" >/home/sunjimeng/Documents/core.log 將找到的文件加上相對路徑和自定義字符串輸出到另一個文件中

[root@localhost Document]# cat all.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
[root@localhost Document]# cd ./
[root@localhost Document]# cd ../
[root@localhost sunjimeng]# find ./Document -name "all.txt" -print | xargs echo "Success" >/home/sunjimeng/Documents/core.log
[root@localhost sunjimeng]# cat /home/sunjimeng/Documents/core.log   //可以看出上個命令將什麼拷進了自定義文件中
Success ./Document/all.txt
[root@localhost sunjimeng]# find ./Document -name "all.txt"
./Document/all.txt
6)[root@localhost sunjimeng]# find ./Document -name "all.txt" | xargs cat >/home/sunjimeng/Documents/t3.txt 將找到的文件內容拷貝到另一個文件中

[root@localhost Document]# cat all.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
[root@localhost sunjimeng]# find ./Document -name "all.txt" | xargs cat >/home/sunjimeng/Documents/t3.txt  //(5)輸出的是自定義內容,而這個命令是把找到的文件內容拷貝一份到自定義文件中
[root@localhost sunjimeng]# cat /home/sunjimeng/Documents/t3.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
7)[root@localhost sunjimeng]# find ./ -type f | xargs ls -l | awk 'BEGIN{size=0}{size+=$5};END{print size}' 統計當前目錄下所有文件的大小,含子目錄,精確到字節

[root@localhost sunjimeng]# find ./ -type f | xargs ls -l | awk 'BEGIN{size=0}{size+=$5};END{print size}'
5173736
8)[root@localhost Documents]# find . -type f -print | xargs grep "Lost"        查找find找到的文件中有沒有包含"Lost"字符串的

[root@localhost Documents]# find . -type f -print | xargs grep "t3.txt" //雖然當前目錄下有t3.txt,但查詢的結果卻為空
[root@localhost Documents]# find . -type f -print
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
./find
./core.log
./t3.txt
[root@localhost Documents]# cat less1
Lost means Get!

No losing No getting!

End!
[root@localhost Documents]# find . -type f -print | xargs grep "Lost"       //表明它是根據文件內容進行匹配的,而不是根據查找到的文件的名字。
./less1:Lost means Get!

9)[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt      提醒是否執行find後面的其他命令

[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt             這裡用mv命令出了錯誤,後面解決!
mv t2.txt ./t1.txt ?...n
[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt
mv t2.txt ./t1.txt ?...y
mv: 無法獲取"t2.txt" 的文件狀態(stat): 沒有那個文件或目錄

Copyright © Linux教程網 All Rights Reserved