linux系統(tǒng)find命令的用法

本篇內容介紹了“l(fā)inux系統(tǒng)find命令的用法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

本篇內容介紹了“l(fā)inux系統(tǒng)find命令的用法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

創(chuàng)新互聯(lián)專注于湞江企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設,商城網(wǎng)站建設。湞江網(wǎng)站建設公司,為湞江等地區(qū)提供建站服務。全流程按需求定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

錯誤信息通常是“參數(shù)列太長”或“參數(shù)列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。 

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

在有些系統(tǒng)中,使用-exec選項會為處理每一個匹配到的文件而發(fā)起一個相應的進程,并非將匹配到的文件全部作為參數(shù)一次執(zhí)行;這樣在有些情況下就會出現(xiàn)進程過多,系統(tǒng)性能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數(shù),還是分批取得參數(shù),以及每一次獲取參數(shù)的數(shù)目都會根據(jù)該命令的選項及系統(tǒng)內核中相應的可調參數(shù)來確定。

使用實例:
實例1: 查找系統(tǒng)中的每一個普通文件,然后使用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.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]#

實例2:在整個系統(tǒng)中查找內存信息轉儲文件(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.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:在當前目錄下查找所有用戶具有讀、寫和執(zhí)行權限的文件,并收回相應的寫權限

命令:

代碼如下:

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
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]#
說明:

執(zhí)行命令后,文件夾scf、test3和test4的權限都發(fā)生改變
實例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執(zhí)行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.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
總計 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
總計 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后執(zhí)行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 -f
rm -f

[root@pdtest4]#
說明:

-l1是一次處理一個;-t是處理之前打印出命令
實例8:使用-i參數(shù)默認的前面輸出用{}代替,-I參數(shù)可以指定其他代替字符,如例子中的[]
命令:

輸出:

代碼如下:
[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]#
說明:
使用-i參數(shù)默認的前面輸出用{}代替,-I參數(shù)可以指定其他代替字符,如例子中的[]
實例9:xargs的-p參數(shù)的使用

命令:

代碼如下:

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.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]#
說明:

-p參數(shù)會提示讓你確認是否執(zhí)行后面的命令,y執(zhí)行,n不執(zhí)行。

文章標題:linux系統(tǒng)find命令的用法
當前路徑:http://www.muchs.cn/article6/copig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、網(wǎng)站營銷、標簽優(yōu)化、服務器托管、微信小程序、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設