學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux中的find命令

linux中的find命令

時(shí)間: 佳洲1085 分享

linux中的find命令

  Linux中的find命令主要用于查找,下面由學(xué)習(xí)啦小編為大家整理了linux中的find命令的相關(guān)知識(shí),希望對(duì)大家有幫助!

  linux中的find命令詳解

  Linux下find命令在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。Linux下find命令提供了相當(dāng)多的查找條件,功能很強(qiáng)大。由于find具有強(qiáng)大的功能,所以它的選項(xiàng)也很多,其中大部分選項(xiàng)都值得我們花時(shí)間來(lái)了解一下。即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS),find命令在該文件系統(tǒng)中同樣有效,只你具有相應(yīng)的權(quán)限。 在運(yùn)行一個(gè)非常消耗資源的find命令時(shí),很多人都傾向于把它放在后臺(tái)執(zhí)行,因?yàn)楸闅v一個(gè)大的文件系統(tǒng)可能會(huì)花費(fèi)很長(zhǎng)的時(shí)間(這里是指30G字節(jié)以上的文件系統(tǒng))。

  1.命令格式:

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

  2.命令功能:

  用于在文件樹(shù)種查找文件,并作出相應(yīng)的處理

  3.命令參數(shù):

  pathname: find命令所查找的目錄路徑。例如用.來(lái)表示當(dāng)前目錄,用/來(lái)表示系統(tǒng)根目錄。

  -print: find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出。

  -exec: find命令對(duì)匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為'command' { } \;,注意{ }和\;之間的空格。

  -ok: 和-exec的作用相同,只不過(guò)以一種更為安全的模式來(lái)執(zhí)行該參數(shù)所給出的shell命令,在執(zhí)行每一個(gè)命令之前,都會(huì)給出提示,讓用戶來(lái)確定是否執(zhí)行。

  4.命令選項(xiàng):

  -name 按照文件名查找文件。

  -perm 按照文件權(quán)限來(lái)查找文件。

  -prune 使用這一選項(xiàng)可以使find命令不在當(dāng)前指定的目錄中查找,如果同時(shí)使用-depth選項(xiàng),那么-prune將被find命令忽略。

  -user 按照文件屬主來(lái)查找文件。

  -group 按照文件所屬的組來(lái)查找文件。

  -mtime -n +n 按照文件的更改時(shí)間來(lái)查找文件, - n表示文件更改時(shí)間距現(xiàn)在n天以內(nèi),+ n表示文件更改時(shí)間距現(xiàn)在n天以前。find命令還有-atime和-ctime 選項(xiàng),但它們都和-m time選項(xiàng)。

  -nogroup 查找無(wú)有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。

  -nouser 查找無(wú)有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。

  -newer file1 ! file2 查找更改時(shí)間比文件file1新但比文件file2舊的文件。

  -type 查找某一類型的文件,諸如:

  b - 塊設(shè)備文件。

  d - 目錄。

  c - 字符設(shè)備文件。

  p - 管道文件。

  l - 符號(hào)鏈接文件。

  f - 普通文件。

  -size n:[c] 查找文件長(zhǎng)度為n塊的文件,帶有c時(shí)表示文件長(zhǎng)度以字節(jié)計(jì)。-depth:在查找文件時(shí),首先查找當(dāng)前目錄中的文件,然后再在其子目錄中查找。

  -fstype:查找位于某一類型文件系統(tǒng)中的文件,這些文件系統(tǒng)類型通??梢栽谂渲梦募?etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息。

  -mount:在查找文件時(shí)不跨越文件系統(tǒng)mount點(diǎn)。

  -follow:如果find命令遇到符號(hào)鏈接文件,就跟蹤至鏈接所指向的文件。

  -cpio:對(duì)匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中。

  另外,下面三個(gè)的區(qū)別:

  -amin n 查找系統(tǒng)中最后N分鐘訪問(wèn)的文件

  -atime n 查找系統(tǒng)中最后n*24小時(shí)訪問(wèn)的文件

  -cmin n 查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件

  -ctime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件狀態(tài)的文件

  -mmin n 查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件

  -mtime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件數(shù)據(jù)的文件

  linux中的find命令使用實(shí)例

  實(shí)例1:查找指定時(shí)間內(nèi)修改過(guò)的文件

  命令:

  find -atime -2

  輸出:

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

  .

  ./logs/monitor

  ./.bashrc

  ./.bash_profile

  ./.bash_history

  說(shuō)明:

  超找48小時(shí)內(nèi)修改過(guò)的文件

  實(shí)例2:根據(jù)關(guān)鍵字查找

  命令:

  find . -name "*.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

  說(shuō)明:

  在當(dāng)前目錄查找 以.log結(jié)尾的文件。 ". "代表當(dāng)前目錄

  實(shí)例3:按照目錄或文件的權(quán)限來(lái)查找文件

  命令:

  find /opt/soft/test/ -perm 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

  說(shuō)明:

  查找/opt/soft/test/目錄下 權(quán)限為 777的文件

  實(shí)例4:按類型查找

  命令:

  find . -type f -name "*.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

  [root@localhost test]#

  說(shuō)明:

  查找當(dāng)目錄,以.log結(jié)尾的普通文件

  實(shí)例5:查找當(dāng)前所有目錄并排序

  命令:

  find . -type d | sort

  輸出:

  [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

  [root@localhost test]#

  實(shí)例6:按大小查找文件

  命令:

  find . -size +1000c -print

  輸出:

  [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

  [root@localhost test]#

  說(shuō)明:

  查找當(dāng)前目錄大于1K的文件

3625730