linux 命令——19 find (轉)

來源:互聯網
上載者:User

標籤:

Linux 下find命令在目錄結構中搜尋檔案,並執行指定的操作。Linux下find命令提供了相當多的尋找條件,功能很強大。由於find具有強大的功能,所 以它的選項也很多,其中大部分選項都值得我們花時間來瞭解一下。即使系統中含有網路檔案系統( NFS),find命令在該檔案系統中同樣有效,只你具有 相應的許可權。 在運行一個非常消耗資源的find命令時,很多人都傾向於把它放在後台執行,因為遍曆一個大的檔案系統可能會花費很長的時間(這裡是指 30G位元組以上的檔案系統)。

1.命令格式:

find pathname -options [-print -exec -ok ...]

2.命令功能:

用於在檔案樹種尋找檔案,並作出相應的處理 

3.命令參數:

pathname: find命令所尋找的目錄路徑。例如用.來表示目前的目錄,用/來表示系統根目錄。 

-print: find命令將匹配的檔案輸出到標準輸出。 

-exec: find命令對匹配的檔案執行該參數所給出的shell命令。相應命令的形式為‘command‘ {  } \;,注意{   }和\;之間的空格。 

-ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓使用者來確定是否執行。

4.命令選項:

-name   按照檔案名稱尋找檔案。

-perm   按照檔案許可權來尋找檔案。

-prune  使用這一選項可以使find命令不在當前指定的目錄中尋找,如果同時使用-depth選項,那麼-prune將被find命令忽略。

-user   按照檔案屬主來尋找檔案。

-group  按照檔案所屬的組來尋找檔案。

-mtime -n +n  按照檔案的更改時間來尋找檔案, - n表示檔案更改時間距現在n天以內,+ n表示檔案更改時間距現在n天以前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。

-nogroup  尋找無有效所屬組的檔案,即該檔案所屬的組在/etc/groups中不存在。

-nouser   尋找無有效屬主的檔案,即該檔案的屬主在/etc/passwd中不存在。

-newer file1 ! file2  尋找更改時間比檔案file1新但比檔案file2舊的檔案。

-type  尋找某一類型的檔案,諸如:

b - 塊裝置檔案。

d - 目錄。

c - 字元裝置檔案。

p - 管道檔案。

l - 符號連結檔案。

f - 普通檔案。

-size n:[c] 尋找檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計。-depth:在尋找檔案時,首先尋找目前的目錄中的檔案,然後再在其子目錄中尋找。

-fstype:尋找位於某一類型檔案系統中的檔案,這些檔案系統類型通常可以在設定檔/etc/fstab中找到,該設定檔中包含了本系統中有關檔案系統的資訊。

-mount:在尋找檔案時不跨越檔案系統mount點。

-follow:如果find命令遇到符號連結檔案,就跟蹤至連結所指向的檔案。

-cpio:對匹配的檔案使用cpio命令,將這些檔案備份到磁帶裝置中。

另外,下面三個的區別:

-amin n   尋找系統中最後N分鐘訪問的檔案

-atime n  尋找系統中最後n*24小時訪問的檔案

-cmin n   尋找系統中最後N分鐘被改變檔案狀態的檔案

-ctime n  尋找系統中最後n*24小時被改變檔案狀態的檔案

-mmin n   尋找系統中最後N分鐘被改變檔案資料的檔案

-mtime n  尋找系統中最後n*24小時被改變檔案資料的檔案

5.使用執行個體:

執行個體1:尋找指定時間內修改過的檔案 

命令:

           find -atime -2

輸出:

[[email protected] ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

說明:

超找48小時內修改過的檔案 

 

執行個體2:根據關鍵字尋找 

命令:

find . -name "*.log"

輸出:

[[email protected] test]# find . -name "*.log" 

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

說明:

在目前的目錄尋找 以.log結尾的檔案。 ". "代表目前的目錄 

 

執行個體3:按照目錄或檔案的許可權來尋找檔案

命令:

find /opt/soft/test/ -perm 777

輸出:

[[email protected] test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

說明: 

尋找/opt/soft/test/目錄下 許可權為 777的檔案

 

執行個體4:按類型尋找 

命令:

find . -type f -name "*.log"

輸出:

[[email protected] test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[[email protected] test]#

說明:

尋找當目錄,以.log結尾的普通檔案 

 

 

執行個體5:尋找當前所有目錄並排序

命令:

find . -type d | sort

輸出:

[[email protected] test]# find . -type d | sort

.

./scf

./scf/bin

./scf/doc

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/info

./scf/service/deploy/product

./test3

./test4

./test5

./test5/test3

[[email protected] test]#

 

 

執行個體6:按大小尋找檔案

命令:

find . -size +1000c -print

輸出:

[[email protected] test]#  find . -size +1000c -print

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

./test3

[[email protected] test]#

說明:

尋找目前的目錄大於1K的檔案

linux 命令——19 find (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.