Linux中的find命令在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。Linux下find命令提供了相當(dāng)多的查找條件,功能很強(qiáng)大。由于find具有強(qiáng)大的功能,所以它的選項也很多,其中大部分選項都值得我們花時間來了解一下。即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS),find命令在該文件系統(tǒng)中同樣有效,只你具有相應(yīng)的權(quán)限。 在運(yùn)行一個非常消耗資源的find命令時,很多人都傾向于把它放在后臺執(zhí)行,因為遍歷一個大的文件系統(tǒng)可能會花費(fèi)很長的時間(這里是指30G字節(jié)以上的文件系統(tǒng))。
find pathname -options [-print -exec -ok ...]
用于在文件樹種查找文件,并作出相應(yīng)的處理
pathname - find命令所查找的目錄路徑。例如用.來表示當(dāng)前目錄,用/來表示系統(tǒng)根目錄。 -print - find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出。 -exec - find命令對匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為’command’ { } \;,注意{ }和\;之間的空格。 -ok - 和-exec的作用相同,只不過以一種更為安全的模式來執(zhí)行該參數(shù)所給出的shell命令,在執(zhí)行每一個命令之前,都會給出提示,讓用戶來確定是否執(zhí)行。-name 按照文件名查找文件。-perm 按照文件權(quán)限來查找文件。-prune 使用這一選項可以使find命令不在當(dāng)前指定的目錄中查找,如果同時使用-depth選項,那么-prune將被find命令忽略。-user 按照文件屬主來查找文件。-group 按照文件所屬的組來查找文件。-mtime -n +n 按照文件的更改時間來查找文件, - n表示文件更改時間距現(xiàn)在n天以內(nèi),+ n表示文件更改時間距現(xiàn)在n天以前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。-nouser 查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。-newer file1 ! file2 查找更改時間比文件file1新但比文件file2舊的文件。-type 查找某一類型的文件,諸如:b - 塊設(shè)備文件。d - 目錄。c - 字符設(shè)備文件。p - 管道文件。l - 符號鏈接文件。f - 普通文件。-size n:[c] 查找文件長度為n塊的文件,帶有c時表示文件長度以字節(jié)計。-depth:在查找文件時,首先查找當(dāng)前目錄中的文件,然后再在其子目錄中查找。-fstype:查找位于某一類型文件系統(tǒng)中的文件,這些文件系統(tǒng)類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息。-mount:在查找文件時不跨越文件系統(tǒng)mount點(diǎn)。-follow:如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。-cpio:對匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中。另外,下面三個的區(qū)別:
-amin n 查找系統(tǒng)中最后N分鐘訪問的文件atime n 查找系統(tǒng)中最后n*24小時訪問的文件-cmin n 查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件-ctime n 查找系統(tǒng)中最后n*24小時被改變文件狀態(tài)的文件-mmin n 查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件-mtime n 查找系統(tǒng)中最后n*24小時被改變文件數(shù)據(jù)的文件命令:
find -atime -2
執(zhí)行演示及輸出:
[yiibai@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
[yiibai@localhost ~]$
說明:超找48小時內(nèi)修改過的文件
命令:
find . -name "*.log"
執(zhí)行演示及輸出:
[yiibai@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
[yiibai@localhost ~]$
說明:在當(dāng)前目錄查找 以.log結(jié)尾的文件。 “. “代表當(dāng)前目錄
命令:
find /home/yiibai -perm 777
執(zhí)行演示及輸出:
[yiibai@localhost ~]$ find /home/yiibai -perm 777
/home/yiibai/mylog_link.log
[yiibai@localhost ~]$
說明: 查找/opt/soft/test/目錄下 權(quán)限為 777的文件
命令:
find . -type f -name "*.log"
演示執(zhí)行及輸出:
[yiibai@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
[yiibai@localhost ~]$
說明:查找當(dāng)目錄,以.log結(jié)尾的普通文件
命令:
find . -type d | sort
演示執(zhí)行及輸出:
[yiibai@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
命令:
find . -size +1000c -print
演示執(zhí)行及輸出:
[yiibai@localhost ~]$ find . -size +1000c -print
./test/is line 1.
./test/t
./test/t test]$ cat log.log
./test/ping.log
[yiibai@localhost ~]$
說明:查找當(dāng)前目錄大于
1K的文件