Linux文件查找命令總結(jié)(下篇)
目錄
- 前言
- find 命令的參數(shù)詳解
- 使用name選項(xiàng)
- 用perm選項(xiàng)
- 忽略某個(gè)目錄
- 使用find查找文件的時(shí)候怎么避開(kāi)某個(gè)文件目錄
- 在test 目錄下查找不在test4子目錄之內(nèi)的所有文件
- 查找某一確定文件,-name等選項(xiàng)加在-o 之后
- 使用user和nouser選項(xiàng)
- 在$HOME目錄中查找文件屬主為peida的文件
- 在/etc目錄下查找文件屬主為peida的文件
- 為了查找屬主帳戶(hù)已經(jīng)被刪除的文件,可以使用-nouser選項(xiàng)。在/home目錄下查找所有的這類(lèi)文件
- 使用group和nogroup選項(xiàng)
- 按照更改時(shí)間或訪問(wèn)時(shí)間等查找文件
- 查找比某個(gè)文件新或舊的文件
- 查找更改時(shí)間比文件log2012.log新但比文件log2017.log舊的文件
- 查找更改時(shí)間在比log2012.log文件新的文件
- 使用type選項(xiàng)
- 在/etc目錄下查找所有的目錄
- 在當(dāng)前目錄下查找除目錄以外的所有類(lèi)型的文件
- 在/etc目錄下查找所有的符號(hào)鏈接文件
- 使用size選項(xiàng)
- 在當(dāng)前目錄下查找文件長(zhǎng)度大于1 M字節(jié)的文件
- 在/home/apache目錄下查找文件長(zhǎng)度恰好為100字節(jié)的文件
- 在當(dāng)前目錄下查找長(zhǎng)度超過(guò)10塊的文件(一塊等于512字節(jié))
- 使用depth選項(xiàng)
- find命令從文件系統(tǒng)的根目錄開(kāi)始,查找一個(gè)名為CON.FILE的文件
- 使用mount選項(xiàng)
- 從當(dāng)前目錄開(kāi)始查找位于本文件系統(tǒng)中文件名以XC結(jié)尾的文件
前言
關(guān)于Linux文件查找命令總結(jié)我們分別介紹了which命令、whereis命令、locate命令、find命令這四個(gè)命令,本篇向大家介紹的是find命令。
find 命令的參數(shù)詳解
find一些常用參數(shù)的一些常用實(shí)例和一些具體用法和注意事項(xiàng)。
使用name選項(xiàng)
文件名選項(xiàng)是find命令最常用的選項(xiàng),要么單獨(dú)使用該選項(xiàng),要么和其他選項(xiàng)一起使用。
可以使用某種文件名模式來(lái)匹配文件,記住要用引號(hào)將文件名模式引起來(lái)。
不管當(dāng)前路徑是什么,如果想要在自己的根目錄 H O M E 中 查 找 文 件 名 符 合 ∗ . l o g 的 文 件 , 使 用 作 為 ′ p a t h n a m e ′ 參 數(shù) , 波 浪 號(hào) 代 表 了 你 的 HOME中查找文件名符合*.log的文件,使用~作為 'pathname'參數(shù),波浪號(hào)~代表了你的 HOME中查找文件名符合∗.log的文件,使用 作為′pathname′參數(shù),波浪號(hào) 代表了你的HOME目錄。
find ~ -name "*.log" -print
想要在當(dāng)前目錄及子目錄中查找所有的‘ *.log‘文件,可以用:
find . -name "*.log" -print
想要的當(dāng)前目錄及子目錄中查找文件名以一個(gè)大寫(xiě)字母開(kāi)頭的文件,可以用:
find . -name "[A-Z]*" -print
想要在/etc目錄中查找文件名以host開(kāi)頭的文件,可以用:
find /etc -name "host*" -print
想要查找$HOME目錄中的文件,可以用:
find ~ -name "*" -print 或find . -print
要想讓系統(tǒng)高負(fù)荷運(yùn)行,就從根目錄開(kāi)始查找所有的文件。
find / -name "*" -print
如果想在當(dāng)前目錄查找文件名以一個(gè)個(gè)小寫(xiě)字母開(kāi)頭,最后是4到9加上.log結(jié)束的文件:
命令:
find . -name "[a-z]*[4-9].log" -print
輸出:
[root@localhost test]# ll 總計(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]#
用perm選項(xiàng)
按照文件權(quán)限模式用-perm選項(xiàng),按文件權(quán)限模式來(lái)查找文件的話。
最好使用八進(jìn)制的權(quán)限表示法。
如在當(dāng)前目錄下查找文件權(quán)限位為755的文件,即文件屬主可以讀、寫(xiě)、執(zhí)行,其他用戶(hù)可以讀、執(zhí)行的文件,可以用:
[root@localhost test]# find . -perm 755 -print . ./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,
命令:
find . -perm -005
輸出:
[root@localhost test]# ll 總計(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]#
忽略某個(gè)目錄
如果在查找文件時(shí)希望忽略某個(gè)目錄,因?yàn)槟阒滥莻€(gè)目錄中沒(méi)有你所要查找的文件,那么可以使用-prune選項(xiàng)來(lái)指出需要忽略的目錄。
在使用-prune選項(xiàng)時(shí)要當(dāng)心,因?yàn)槿绻阃瑫r(shí)使用了-depth選項(xiàng),那么-prune選項(xiàng)就會(huì)被find命令忽略。
如果希望在test目錄下查找文件,但不希望在test/test3目錄下查找,可以用:
命令:
find test -path "test/test3" -prune -o -print
輸出:
[root@localhost soft]# find test -path "test/test3" -prune -o -print 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]#
使用find查找文件的時(shí)候怎么避開(kāi)某個(gè)文件目錄
在test 目錄下查找不在test4子目錄之內(nèi)的所有文件
命令:
find test -path "test/test4" -prune -o -print
輸出:
[root@localhost soft]# find test 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]#
說(shuō)明:
find [-path …] [expression]
在路徑列表的后面的是表達(dá)式
-path “test” -prune -o -print 是 -path “test” -a -prune -o -print 的簡(jiǎn)寫(xiě)表達(dá)式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類(lèi)似如果
-path “test” 為真,則求值 -prune , -prune 返回真,與邏輯表達(dá)式為真;
否則不求值 -prune,與邏輯表達(dá)式為假。如果 -path “test” -a -prune 為假,則求值 -print ,-print返回真,或邏輯表達(dá)式為真;
否則不求值 -print,或邏輯表達(dá)式為真。
這個(gè)表達(dá)式組合特例可以用偽碼寫(xiě)為:
if -path “test” then
-prune
else
避開(kāi)多個(gè)文件夾:
命令:
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -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 [root@localhost soft]#
說(shuō)明:
圓括號(hào)表示表達(dá)式的結(jié)合。 \ 表示引用,即指示 shell 不對(duì)后面的字符作特殊解釋?zhuān)艚o find 命令去解釋其意義。
查找某一確定文件,-name等選項(xiàng)加在-o 之后
命令:
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print test/log2014.log test/log2015.log test/log2013.log test/log2012.log [root@localhost soft]#
使用user和nouser選項(xiàng)
按文件屬主查找文件:
在$HOME目錄中查找文件屬主為peida的文件
命令:
find ~ -user peida -print
在/etc目錄下查找文件屬主為peida的文件
命令:
find /etc -user peida -print
說(shuō)明:
為了查找屬主帳戶(hù)已經(jīng)被刪除的文件,可以使用-nouser選項(xiàng)。在/home目錄下查找所有的這類(lèi)文件
命令:
find /home -nouser -print
說(shuō)明:
這樣就能夠找到那些屬主在/etc/passwd文件中沒(méi)有有效帳戶(hù)的文件。
在使用-nouser選項(xiàng)時(shí),不必給出用戶(hù)名; find命令能夠?yàn)槟阃瓿上鄳?yīng)的工作。
使用group和nogroup選項(xiàng)
就像user和nouser選項(xiàng)一樣,針對(duì)文件所屬于的用戶(hù)組, find命令也具有同樣的選項(xiàng),為了在/apps目錄下查找屬于gem用戶(hù)組的文件,可以用:
find /apps -group gem -print
要查找沒(méi)有有效所屬用戶(hù)組的所有文件,可以使用nogroup選項(xiàng)。
下面的find命令從文件系統(tǒng)的根目錄處查找這樣的文件:
find / -nogroup-print
按照更改時(shí)間或訪問(wèn)時(shí)間等查找文件
如果希望按照更改時(shí)間來(lái)查找文件,可以使用mtime,atime或ctime選項(xiàng)。
如果系統(tǒng)突然沒(méi)有可用空間了,很有可能某一個(gè)文件的長(zhǎng)度在此期間增長(zhǎng)迅速,這時(shí)就可以用mtime選項(xiàng)來(lái)查找這樣的文件。
用減號(hào)-來(lái)限定更改時(shí)間在距今n日以?xún)?nèi)的文件,而用加號(hào)+來(lái)限定更改時(shí)間在距今n日以前的文件。
希望在系統(tǒng)根目錄下查找更改時(shí)間在5日以?xún)?nèi)的文件,可以用:
find / -mtime -5 -print
為了在/var/adm目錄下查找更改時(shí)間在3日以前的文件,可以用:
find /var/adm -mtime +3 -print
查找比某個(gè)文件新或舊的文件
如果希望查找更改時(shí)間比某個(gè)文件新但比另一個(gè)文件舊的所有文件,可以使用-newer選項(xiàng)。
它的一般形式為:
newest_file_name ! oldest_file_name
其中,!是邏輯非符號(hào)。
查找更改時(shí)間比文件log2012.log新但比文件log2017.log舊的文件
命令:
find -newer log2012.log ! -newer log2017.log
輸出:
[root@localhost test]# ll 總計(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í)間在比log2012.log文件新的文件
命令:
find . -newer log2012.log -print
輸出:
[root@localhost test]# find -newer log2012.log . ./log2015.log ./log2017.log ./log2016.log ./test3 [root@localhost test]#
使用type選項(xiàng)
在/etc目錄下查找所有的目錄
命令:
find /etc -type d -print
在當(dāng)前目錄下查找除目錄以外的所有類(lèi)型的文件
命令:
find . ! -type d -print
在/etc目錄下查找所有的符號(hào)鏈接文件
命令:
find /etc -type l -print
使用size選項(xiàng)
可以按照文件長(zhǎng)度來(lái)查找文件,這里所指的文件長(zhǎng)度既可以用塊(block)來(lái)計(jì)量,也可以用字節(jié)來(lái)計(jì)量。
以字節(jié)計(jì)量文件長(zhǎng)度的表達(dá)形式為N c;以塊計(jì)量文件長(zhǎng)度只用數(shù)字表示即可。
在按照文件長(zhǎng)度查找文件時(shí),一般使用這種以字節(jié)表示的文件長(zhǎng)度,在查看文件系統(tǒng)的大小,因?yàn)檫@時(shí)使用塊來(lái)計(jì)量更容易轉(zhuǎn)換。
在當(dāng)前目錄下查找文件長(zhǎng)度大于1 M字節(jié)的文件
命令:
find . -size +1000000c -print
在/home/apache目錄下查找文件長(zhǎng)度恰好為100字節(jié)的文件
命令:
find /home/apache -size 100c -print
在當(dāng)前目錄下查找長(zhǎng)度超過(guò)10塊的文件(一塊等于512字節(jié))
命令:
find . -size +10 -print
使用depth選項(xiàng)
在使用find命令時(shí),可能希望先匹配所有的文件,再在子目錄中查找。
使用depth選項(xiàng)就可以使find命令這樣做。這樣做的一個(gè)原因就是,當(dāng)在使用find命令向磁帶上備份文件系統(tǒng)時(shí),希望首先備份所有的文件,其次再備份子目錄中的文件。
find命令從文件系統(tǒng)的根目錄開(kāi)始,查找一個(gè)名為CON.FILE的文件
命令:
find / -name "CON.FILE" -depth -print
說(shuō)明:
它將首先匹配所有的文件然后再進(jìn)入子目錄中查找
使用mount選項(xiàng)
在當(dāng)前的文件系統(tǒng)中查找文件(不進(jìn)入其他文件系統(tǒng)),可以使用find命令的mount選項(xiàng)。
從當(dāng)前目錄開(kāi)始查找位于本文件系統(tǒng)中文件名以XC結(jié)尾的文件
命令:
find . -name "*.XC" -mount -print
到此這篇關(guān)于Linux文件查找命令總結(jié)(下篇)的文章就介紹到這了,相關(guān)內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
