linux命令之find命令的12個(gè)常用參數(shù)詳解(含具體用法和注意事項(xiàng))
1.使用name選項(xiàng):
文件名選項(xiàng)是find命令最常用的選項(xiàng),要么單獨(dú)使用該選項(xiàng),要么和其他選項(xiàng)一起使用。
可以使用某種文件名模式來匹配文件,記住要用引號將文件名模式引起來。
不管當(dāng)前路徑是什么,如果想要在自己的根目錄$HOME中查找文件名符合*.log的文件,使用~作為 'pathname'參數(shù),波浪號~代表了你的$HOME目錄。
想要在當(dāng)前目錄及子目錄中查找所有的‘ *.log‘文件,可以用:
想要的當(dāng)前目錄及子目錄中查找文件名以一個(gè)大寫字母開頭的文件,可以用:
想要在/etc目錄中查找文件名以host開頭的文件,可以用:
想要查找$HOME目錄中的文件,可以用:
要想讓系統(tǒng)高負(fù)荷運(yùn)行,就從根目錄開始查找所有的文件。
如果想在當(dāng)前目錄查找文件名以一個(gè)個(gè)小寫字母開頭,最后是4到9加上.log結(jié)束的文件:
命令:
輸出:
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find . -name "[a-z]*[4-9].log" -print
./log2014.log
./log2015.log
./test4/log2014.log
[root@localhost test]#
2.用perm選項(xiàng):
按照文件權(quán)限模式用-perm選項(xiàng),按文件權(quán)限模式來查找文件的話。最好使用八進(jìn)制的權(quán)限表示法。
如在當(dāng)前目錄下查找文件權(quán)限位為755的文件,即文件屬主可以讀、寫、執(zhí)行,其他用戶可以讀、執(zhí)行的文件,可以用:
.
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
[root@localhost test]#
還有一種表達(dá)方法:在八進(jìn)制數(shù)字前面要加一個(gè)橫杠-,表示都匹配,如-007就相當(dāng)于777,-005相當(dāng)于555,
命令:
輸出:
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find . -perm -005
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./test3
[root@localhost test]#
3.忽略某個(gè)目錄:
如果在查找文件時(shí)希望忽略某個(gè)目錄,因?yàn)槟阒滥莻€(gè)目錄中沒有你所要查找的文件,那么可以使用-prune選項(xiàng)來指出需要忽略的目錄。在使用-prune選項(xiàng)時(shí)要當(dāng)心,因?yàn)槿绻阃瑫r(shí)使用了-depth選項(xiàng),那么-prune選項(xiàng)就會(huì)被find命令忽略。如果希望在test目錄下查找文件,但不希望在test/test3目錄下查找,可以用:
命令:
輸出:
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
4.使用find查找文件的時(shí)候怎么避開某個(gè)文件目錄:
實(shí)例1:在test 目錄下查找不在test4子目錄之內(nèi)的所有文件
命令:
輸出:
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]# find test -path "test/test4" -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]#
說明:
在路徑列表的后面的是表達(dá)式
-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的簡寫表達(dá)式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果
-path "test" 為真,則求值 -prune , -prune 返回真,與邏輯表達(dá)式為真;否則不求值 -prune,與邏輯表達(dá)式為假。如果 -path "test" -a -prune 為假,則求值 -print ,-print返回真,或邏輯表達(dá)式為真;否則不求值 -print,或邏輯表達(dá)式為真。
這個(gè)表達(dá)式組合特例可以用偽碼寫為:
if -path "test" then
-prune
else
實(shí)例2:避開多個(gè)文件夾:
命令:
輸出:
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
說明:
圓括號表示表達(dá)式的結(jié)合。 \ 表示引用,即指示 shell 不對后面的字符作特殊解釋,而留給 find 命令去解釋其意義。
實(shí)例3:查找某一確定文件,-name等選項(xiàng)加在-o 之后
命令:
輸出:
test/log2014.log
test/log2015.log
test/log2013.log
test/log2012.log
[root@localhost soft]#
5.使用user和nouser選項(xiàng):
按文件屬主查找文件:
實(shí)例1:在$HOME目錄中查找文件屬主為peida的文件
命令:
實(shí)例2:在/etc目錄下查找文件屬主為peida的文件:
命令:
說明:
實(shí)例3:為了查找屬主帳戶已經(jīng)被刪除的文件,可以使用-nouser選項(xiàng)。在/home目錄下查找所有的這類文件
命令:
說明:
這樣就能夠找到那些屬主在/etc/passwd文件中沒有有效帳戶的文件。在使用-nouser選項(xiàng)時(shí),不必給出用戶名; find命令能夠?yàn)槟阃瓿上鄳?yīng)的工作。
6.使用group和nogroup選項(xiàng):
就像user和nouser選項(xiàng)一樣,針對文件所屬于的用戶組, find命令也具有同樣的選項(xiàng),為了在/apps目錄下查找屬于gem用戶組的文件,可以用:
要查找沒有有效所屬用戶組的所有文件,可以使用nogroup選項(xiàng)。下面的find命令從文件系統(tǒng)的根目錄處查找這樣的文件:
7.按照更改時(shí)間或訪問時(shí)間等查找文件:
如果希望按照更改時(shí)間來查找文件,可以使用mtime,atime或ctime選項(xiàng)。如果系統(tǒng)突然沒有可用空間了,很有可能某一個(gè)文件的長度在此期間增長迅速,這時(shí)就可以用mtime選項(xiàng)來查找這樣的文件。
用減號-來限定更改時(shí)間在距今n日以內(nèi)的文件,而用加號+來限定更改時(shí)間在距今n日以前的文件。
希望在系統(tǒng)根目錄下查找更改時(shí)間在5日以內(nèi)的文件,可以用:
為了在/var/adm目錄下查找更改時(shí)間在3日以前的文件,可以用:
8.查找比某個(gè)文件新或舊的文件:
如果希望查找更改時(shí)間比某個(gè)文件新但比另一個(gè)文件舊的所有文件,可以使用-newer選項(xiàng)。
它的一般形式為:
其中,!是邏輯非符號。
實(shí)例1:查找更改時(shí)間比文件log2012.log新但比文件log2017.log舊的文件
命令:
輸出:
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log
-rw-r--r-- 1 root root 0 11-16 14:43 log2017.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find -newer log2012.log ! -newer log2017.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
實(shí)例2:查找更改時(shí)間在比log2012.log文件新的文件
命令:
輸出:
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
9.使用type選項(xiàng):
實(shí)例1:在/etc目錄下查找所有的目錄
命令:
實(shí)例2:在當(dāng)前目錄下查找除目錄以外的所有類型的文件
命令:
實(shí)例3:在/etc目錄下查找所有的符號鏈接文件
命令:
10.使用size選項(xiàng):
可以按照文件長度來查找文件,這里所指的文件長度既可以用塊(block)來計(jì)量,也可以用字節(jié)來計(jì)量。以字節(jié)計(jì)量文件長度的表達(dá)形式為N c;以塊計(jì)量文件長度只用數(shù)字表示即可。
在按照文件長度查找文件時(shí),一般使用這種以字節(jié)表示的文件長度,在查看文件系統(tǒng)的大小,因?yàn)檫@時(shí)使用塊來計(jì)量更容易轉(zhuǎn)換。
實(shí)例1:在當(dāng)前目錄下查找文件長度大于1 M字節(jié)的文件
命令:
實(shí)例2:在/home/apache目錄下查找文件長度恰好為100字節(jié)的文件:
命令:
實(shí)例3:在當(dāng)前目錄下查找長度超過10塊的文件(一塊等于512字節(jié))
命令:
11.使用depth選項(xiàng):
在使用find命令時(shí),可能希望先匹配所有的文件,再在子目錄中查找。使用depth選項(xiàng)就可以使find命令這樣做。這樣做的一個(gè)原因就是,當(dāng)在使用find命令向磁帶上備份文件系統(tǒng)時(shí),希望首先備份所有的文件,其次再備份子目錄中的文件。
實(shí)例1:find命令從文件系統(tǒng)的根目錄開始,查找一個(gè)名為CON.FILE的文件。
命令:
說明:
它將首先匹配所有的文件然后再進(jìn)入子目錄中查找
12.使用mount選項(xiàng):
在當(dāng)前的文件系統(tǒng)中查找文件(不進(jìn)入其他文件系統(tǒng)),可以使用find命令的mount選項(xiàng)。
實(shí)例1:從當(dāng)前目錄開始查找位于本文件系統(tǒng)中文件名以XC結(jié)尾的文件
命令:
相關(guān)文章
Linux find命令實(shí)例教程 15個(gè)find命令用法
本文介紹了linux下find命令的用法,舉了十五個(gè)find命令的例子,有需要的朋友可以參考下2014-07-12Linux Find命令查找指定時(shí)間范圍內(nèi)的文件的例子
這篇文章主要介紹了Linux Find命令查找指定時(shí)間范圍內(nèi)的文件的例子,需要的朋友可以參考下2014-06-27- 這篇文章主要介紹了Linux find命令10種使用技巧和方法分享,需要的朋友可以參考下2014-04-24
linux系統(tǒng)中find命令使用示例(linux查找文件)
這篇文章主要介紹了linux系統(tǒng)中find命令使用示例(linux查找文件),需要的朋友可以參考下2014-04-23linux系統(tǒng)find命令之xargs使用實(shí)例分享
在使用find命令的-exec選項(xiàng)處理匹配到的文件時(shí), find命令將所有匹配到的文件一起傳遞給exec執(zhí)行。但有些系統(tǒng)對能夠傳遞給exec的命令長度有限制,這樣在find命令運(yùn)行幾分鐘2014-04-09- find是我們很常用的一個(gè)Linux命令,但是我們一般查找出來的并不僅僅是看看而已,還會(huì)有進(jìn)一步的操作,這個(gè)時(shí)候exec的作用就顯現(xiàn)出來了2014-04-08
linux下使用find命令根據(jù)系統(tǒng)時(shí)間查找文件用法
這篇文章主要為大家介紹了find 命令有幾個(gè)用于根據(jù)您系統(tǒng)的時(shí)間戳搜索文件的選項(xiàng),需要的朋友可以參考下2013-11-18- linux的系統(tǒng)中的文件實(shí)在是太多了,怎么樣才能快速而且精確的在linux系統(tǒng)找出你想要的文件呢?Linux 的find命令提供了很強(qiáng)大的功能,學(xué)好find命令的使用,可以給你節(jié)省大量2013-09-23
- Linux中查找文件的命令通常為find命令,find命令能幫助我們在使用,管理Linux的日常事務(wù)中方便的查找出我們需要的文件2013-08-14
- find命令查找比某個(gè)文件新或舊的文件,有需要的朋友可以參考下2013-01-29

