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
執行演示及輸出:
[zaixian@localhost ~]$ find -atime -2
.
./.bash_profile
./.bashrc
./test
./test/log.log
./test/is line 1.
./test/t
./test/t test]$ cat log.log
./test/ping.log
./mylog_link.log
./.lesshst
[zaixian@localhost ~]$
說明:超找48小時內修改過的檔
實例2:根據關鍵字查找
命令:
find . -name "*.log"
執行演示及輸出:
[zaixian@localhost ~]$ find . -name "*.log"
./test/mylog1.log
./test/mylog2.log
./test/log.log
./test/t test]$ cat log.log
./test/ping.log
./test5/mylog.log
./mylog.log
./mylog_link.log
[zaixian@localhost ~]$
說明:在當前目錄查找 以.log結尾的檔。 “. “代表當前目錄
實例3:按照目錄或檔的許可權來查找檔
命令:
find /home/zaixian -perm 777
執行演示及輸出:
[zaixian@localhost ~]$ find /home/zaixian -perm 777
/home/zaixian/mylog_link.log
[zaixian@localhost ~]$
說明: 查找/opt/soft/test/目錄下 許可權為 777的檔
實例4:按類型查找
命令:
find . -type f -name "*.log"
演示執行及輸出:
[zaixian@localhost ~]$ find . -type f -name "*.log"
./test/mylog1.log
./test/mylog2.log
./test/log.log
./test/t test]$ cat log.log
./test/ping.log
./test5/mylog.log
./mylog.log
[zaixian@localhost ~]$
說明:查找當目錄,以.log結尾的普通檔
實例5:查找當前所有目錄並排序
命令:
find . -type d | sort
演示執行及輸出:
[zaixian@localhost ~]$ find . -type d | sort
.
./test
./test1
./test2
./test3
./test3/test4
./test3/test4/test41
./test5
./test5/test3
./test5/test3/test4
./test5/test3/test4/test41
./test-noexists
./test-noexists/test4
./test-noexists/test4/test41
實例6:按大小查找檔
命令:
find . -size +1000c -print
演示執行及輸出:
[zaixian@localhost ~]$ find . -size +1000c -print
./test/is line 1.
./test/t
./test/t test]$ cat log.log
./test/ping.log
[zaixian@localhost ~]$
說明:查找當前目錄大於
1K的檔
上一篇:
locate命令
下一篇:
find命令-exec參數
