Linux命令之find


Linux命令之find

功能說明

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


語法

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

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

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

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

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


參數

-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小時被改變文件數據的文件


實例1

查找指定時間內修改過的文件,查找48小時內修改過的文件 

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history


實例2

按照目錄或文件的權限來查找文件,查找/opt/soft/test/目錄下 權限爲 777的文件

[root@localhost 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


實例3

根據關鍵字查找,在當前目錄查找以.log結尾的文件。 ". "代表當前目錄 

[root@localhost 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


實例4

按類型查找,查找當目錄,以.log結尾的普通文件

[root@localhost 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


實例5

查找當前所有目錄並排序

[root@localhost 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


實例6

按大小查找文件,查找當前目錄大於1K的文件

[root@localhost 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



參考:

http://www.cnblogs.com/peida/archive/2012/12/12/2814048.html

光榮之路微信公衆號:gloryroadtrain

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章