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參數