每天一個linux命令(22):find 命令的引數詳解
find一些常用引數的一些常用例項和一些具體用法和注意事項。
1.使用name選項:
檔名選項是find命令最常用的選項,要麼單獨使用該選項,要麼和其他選項一起使用。 可以使用某種檔名模式來匹配檔案,記住要用引號將檔名模式引起來。 不管當前路徑是什麼,如果想要在自己的根目錄HOME目錄。
find ~ -name “*.log” -print
想要在當前目錄及子目錄中查詢所有的‘ *.log‘檔案,可以用:
find . -name “*.log” -print
想要的當前目錄及子目錄中查詢檔名以一個大寫字母開頭的檔案,可以用:
find . -name “[A-Z]*” -print
想要在/etc目錄中查詢檔名以host開頭的檔案,可以用:
find /etc -name “host*” -print
想要查詢$HOME目錄中的檔案,可以用:
find ~ -name “*” -print 或find . -print
要想讓系統高負荷執行,就從根目錄開始查詢所有的檔案。
find / -name “*” -print
如果想在當前目錄查詢檔名以一個個小寫字母開頭,最後是4到9加上.log結束的檔案:
命令:
find . -name “[a-z]*[4-9].log” -print
輸出:
[[email protected] test]# ll
總計 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
[[email protected] test]# find . -name “[a-z]*[4-9].log” -print
./log2014.log
./log2015.log
./test4/log2014.log
[[email protected] test]#
2.用perm選項:
按照檔案許可權模式用-perm選項,按檔案許可權模式來查詢檔案的話。最好使用八進位制的許可權表示法。
如在當前目錄下查詢檔案許可權位為755的檔案,即檔案屬主可以讀、寫、執行,其他使用者可以讀、執行的檔案,可以用:
[[email protected] 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
[[email protected] test]#
還有一種表達方法:在八進位制數字前面要加一個橫槓-,表示都匹配,如-007就相當於777,-005相當於555,
命令:
find . -perm -005
輸出:
[[email protected] test]# ll
總計 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
[[email protected] 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
[[email protected] test]#
3.忽略某個目錄:
如果在查詢檔案時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查詢的檔案,那麼可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因為如果你同時使用了-depth選項,那麼-prune選項就會被find命令忽略。如果希望在test目錄下查詢檔案,但不希望在test/test3目錄下查詢,可以用:
命令:
find test -path “test/test3” -prune -o -print
輸出:
[[email protected] 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
[[email protected] soft]#
4.使用find查詢檔案的時候怎麼避開某個檔案目錄:
例項1:在test 目錄下查詢不在test4子目錄之內的所有檔案
命令:
find test -path “test/test4” -prune -o -print
輸出:
[[email protected] 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
[[email protected] 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
[[email protected] soft]#
說明:
find [-path ..] [expression]
在路徑列表的後面的是表示式
-path “test” -prune -o -print 是 -path “test” -a -prune -o -print 的簡寫表示式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果
-path “test” 為真,則求值 -prune , -prune 返回真,與邏輯表示式為真;否則不求值 -prune,與邏輯表示式為假。如果 -path “test” -a -prune 為假,則求值 -print ,-print返回真,或邏輯表示式為真;否則不求值 -print,或邏輯表示式為真。
這個表示式組合特例可以用偽碼寫為:
if -path “test” then
-prune
else
例項2:避開多個資料夾:
命令:
find test ( -path test/test4 -o -path test/test3 ) -prune -o -print
輸出:
[[email protected] 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
[[email protected] soft]#
說明:
圓括號表示表示式的結合。 \ 表示引用,即指示 shell 不對後面的字元作特殊解釋,而留給 find 命令去解釋其意義。
例項3:查詢某一確定檔案,-name等選項加在-o 之後
命令:
find test (-path test/test4 -o -path test/test3 ) -prune -o -name “*.log” -print
輸出:
[[email protected] 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
[[email protected] soft]#
5.使用user和nouser選項:
按檔案屬主查詢檔案:
例項1:在$HOME目錄中查詢檔案屬主為peida的檔案
命令:
find ~ -user peida -print
例項2:在/etc目錄下查詢檔案屬主為peida的檔案:
命令:
find /etc -user peida -print
說明:
例項3:為了查詢屬主帳戶已經被刪除的檔案,可以使用-nouser選項。在/home目錄下查詢所有的這類檔案
命令:
find /home -nouser -print
說明:
這樣就能夠找到那些屬主在/etc/passwd檔案中沒有有效帳戶的檔案。在使用-nouser選項時,不必給出使用者名稱; find命令能夠為你完成相應的工作。
6.使用group和nogroup選項:
就像user和nouser選項一樣,針對檔案所屬於的使用者組, find命令也具有同樣的選項,為了在/apps目錄下查詢屬於gem使用者組的檔案,可以用:
find /apps -group gem -print
要查詢沒有有效所屬使用者組的所有檔案,可以使用nogroup選項。下面的find命令從檔案系統的根目錄處查詢這樣的檔案:
find / -nogroup-print
7.按照更改時間或訪問時間等查詢檔案:
如果希望按照更改時間來查詢檔案,可以使用mtime,atime或ctime選項。如果系統突然沒有可用空間了,很有可能某一個檔案的長度在此期間增長迅速,這時就可以用mtime選項來查詢這樣的檔案。
用減號-來限定更改時間在距今n日以內的檔案,而用加號+來限定更改時間在距今n日以前的檔案。
希望在系統根目錄下查詢更改時間在5日以內的檔案,可以用:
find / -mtime -5 -print
為了在/var/adm目錄下查詢更改時間在3日以前的檔案,可以用:
find /var/adm -mtime +3 -print
8.查詢比某個檔案新或舊的檔案:
如果希望查詢更改時間比某個檔案新但比另一個檔案舊的所有檔案,可以使用-newer選項。
它的一般形式為:
newest_file_name ! oldest_file_name
其中,!是邏輯非符號。
例項1:查詢更改時間比檔案log2012.log新但比檔案log2017.log舊的檔案
命令:
find -newer log2012.log ! -newer log2017.log
輸出:
[[email protected]ocalhost test]# ll
總計 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
[[email protected] test]# find -newer log2012.log ! -newer log2017.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[[email protected] test]#
例項2:查詢更改時間在比log2012.log檔案新的檔案
命令:
find . -newer log2012.log -print
輸出:
[[email protected] test]# find -newer log2012.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[[email protected] test]#
9.使用type選項:
例項1:在/etc目錄下查詢所有的目錄
命令:
find /etc -type d -print
例項2:在當前目錄下查詢除目錄以外的所有型別的檔案
命令:
find . ! -type d -print
例項3:在/etc目錄下查詢所有的符號連結檔案
命令:
find /etc -type l -print
10.使用size選項:
可以按照檔案長度來查詢檔案,這裡所指的檔案長度既可以用塊(block)來計量,也可以用位元組來計量。以位元組計量檔案長度的表達形式為N c;以塊計量檔案長度只用數字表示即可。
在按照檔案長度查詢檔案時,一般使用這種以位元組表示的檔案長度,在檢視檔案系統的大小,因為這時使用塊來計量更容易轉換。
例項1:在當前目錄下查詢檔案長度大於1 M位元組的檔案
命令:
find . -size +1000000c -print
例項2:在/home/apache目錄下查詢檔案長度恰好為100位元組的檔案:
命令:
find /home/apache -size 100c -print
例項3:在當前目錄下查詢長度超過10塊的檔案(一塊等於512位元組)
命令:
find . -size +10 -print
11.使用depth選項:
在使用find命令時,可能希望先匹配所有的檔案,再在子目錄中查詢。使用depth選項就可以使find命令這樣做。這樣做的一個原因就是,當在使用find命令向磁帶上備份檔案系統時,希望首先備份所有的檔案,其次再備份子目錄中的檔案。
例項1:find命令從檔案系統的根目錄開始,查詢一個名為CON.FILE的檔案。
命令:
find / -name “CON.FILE” -depth -print
說明:
它將首先匹配所有的檔案然後再進入子目錄中查詢
12.使用mount選項:
在當前的檔案系統中查詢檔案(不進入其他檔案系統),可以使用find命令的mount選項。
例項1:從當前目錄開始查詢位於本檔案系統中檔名以XC結尾的檔案
命令:
find . -name “*.XC” -mount -print
相關推薦
每天一個linux命令(22):find 命令的引數詳解
find一些常用引數的一些常用例項和一些具體用法和注意事項。 1.使用name選項: 檔名選項是find命令最常用的選項,要麼單獨使用該選項,要麼和其他選項一起使用。 可以使用某種檔名模式來匹配檔案,記住要用引號將檔名模式引起來。 不管當前路徑是什麼,如
每天一個linux命令(22):tar命令
soft 小文件 sof linu gunzip 算法 rect 過程 提取文件 通過SSH訪問服務器,難免會要用到壓縮,解壓縮,打包,解包等,這時候tar命令就是是必不可少的一個功能強大的工具。linux中最流行的tar是麻雀雖小,五臟俱全,功能強大。 tar命令可以為l
每天一個linux命令(19):find 命令概覽
PE 根據 根目錄 配置 它的 UC sta deploy sort Linux下find命令在目錄結構中搜索文件,並執行指定的操作。Linux下find命令提供了相當多的查找條件,功能很強大。由於find具有強大的功能,所以它的選項也很多,其中大部分選項都值得我們花時間來
每天一個linux命令(16):tail命令
nvi 系統 strong 維基百科 ron .com linux tro 聯系 版權聲明更新:2017-05-20博主:LuckyAlan聯系:[email protected]/* */聲明:吃水不忘挖井人,轉載請註明出處! 1 文章介紹 本文介紹了Linu
每天一個linux命令(11):cat命令
部分 ron linu mv命令 平臺 linux下 一個 介紹 inux 版權聲明更新:2017-05-15博主:LuckyAlan聯系:[email protected]/* */聲明:吃水不忘挖井人,轉載請註明出處! 1 文章介紹 本文介紹了Linux下面
每天一個linux命令(9):cp命令
系統 lin 維基 介紹 參考 vip 聲明 com 開發平臺 版權聲明更新:2017-05-13博主:LuckyAlan聯系:[email protected]/* */聲明:吃水不忘挖井人,轉載請註明出處! 1 文章介紹 本文介紹了Linux下面的cp命令。
轉每天一個linux命令(4):mkdir命令
指定位置 cnblogs 同名 parent --help pos uri 不存在 必須 linux mkdir 命令用來創建指定的名稱的目錄,要求創建目錄的用戶在當前目錄中具有寫權限,並且指定的目錄名不能是當前目錄中已有的目錄。 1.命令格式: mkdir [選
轉每天一個linux命令(5):rm 命令
每天 幫助信息 rbo 總計 com 刪除 強行 高度 linux中 昨天學習了創建文件和目錄的命令mkdir ,今天學習一下linux中刪除文件和目錄的命令: rm命令。rm是常用的命令,該命令的功能為刪除一個目錄中的一個或多個文件或目錄,它也可以將某個目錄及其下的所有文
每天一個linux命令(13):less 命令
文件中 參數 使用 ech height 查看進程 str idt 目錄 less 工具也是對文件或其它輸出進行分頁顯示的工具,應該說是linux正統查看文件內容的工具,功能極其強大。less 的用法比起 more 更加的有彈性。在 more 的時候,我們並沒有辦法向前面翻
每天一個linux命令(18):locate 命令
一次 clu ado 模式 pda -o gic style art locate 讓使用者可以很快速的搜尋檔案系統內是否有指定的檔案。其方法是先建立一個包括系統內所有檔案名稱及路徑的數據庫,之後當尋找時就只需查詢這個數據庫,而不必實際深入檔案系統之中了。在一般的 dis
每天一個linux命令(1):scp 命令
filename use des 本地文件 lin oca www tro 服務器 scp命令主要用於兩個服務器之間文件的傳輸。 1、從服務器下載文件 scp [email protected]:/path/filename /tmp/local_destin
(轉)每天一個linux命令(9):touch 命令
cal log bsp 參考 一個 ati linux命令 包括 ces linux的touch命令不常用,一般在使用make的時候可能會用到,用來修改文件時間戳,或者新建一個不存在的文件。 1 基本使用 1.命令格式: touch [選項]... 文件... 2.
每天一個linux命令(50):crontab命令
指定 檢查 var 編輯 特殊字符 post rip 標準輸入 運行時間 前一天學習了 at 命令是針對僅運行一次的任務,循環運行的例行性計劃任務,linux系統則是由 cron (crond) 這個系統服務來控制的。Linux 系統上面原本就有非常多的計劃性工作
每天一個linux命令(17):whereis 命令
數據 幫助 參數 執行 localhost root 一個數據庫 usr 位置 whereis命令只能用於程序名的搜索,而且只搜索二進制文件(參數-b)、man說明文件(參數-m)和源代碼文件(參數-s)。如果省略參數,則返回所有信息。 和find相比,whereis查找的
每天一個linux命令(16):which命令
哪裏 使用實例 找文件 為什麽 ID 文件 use image sbin 我們經常在linux要查找某個文件,但不知道放在哪裏了,可以使用下面的一些命令來搜索: which 查看可執行文件的位置。 whereis 查看文件的位置。
每天一個linux命令(37):date命令
每天一個linux命令(37):date命令 在linux環境中,不管是程式設計還是其他維護,時間是必不可少的,也經常會用到時間的運算,熟練運用date命令來表示自己想要表示的時間,肯定可以給自己的工作帶來諸多方便。 1.命令格式: &nbs
每天一個linux命令(36):diff 命令
diff 命令是 linux上非常重要的工具,用於比較檔案的內容,特別是比較兩個版本不同的檔案以找到改動的地方。diff在命令列中列印每一個行的改動。最新版本的diff還支援二進位制檔案。diff程式的輸出被稱為補丁 (patch),因為Linux系
每天一個linux命令(35):ln 命令
ln是linux中又一個非常重要命令,它的功能是為某一個檔案在另外一個位置建立一個同步的連結.當我們需要在不同的目錄,用到相同的檔案時,我們不需要在每一個需要的目錄下都放一個必須相同的檔案,我們只要在某個固定的目錄,放上該檔案,然後在 其它的目錄下用ln命令連結(link)它就可以,不必重複的佔用磁碟空間。
每天一個linux命令(18):rm 命令
今天學習一下linux中刪除檔案和目錄的命令: rm命令。rm是常用的命令,該命令的功能為刪除一個目錄中的一個或多個檔案或目錄,它也可以將某個目錄及其下的所有檔案及子目錄均刪除。對於連結檔案,只是刪除了連結,原有檔案均保持不變。 rm是一個危險的命令,使用的時候要特別當心,尤其對於新手
每天一個linux命令(15):tail 命令,實時列印TOMCAT日誌
tail 命令從指定點開始將檔案寫到標準輸出.使用tail命令的-f選項可以方便的查閱正在改變的日誌檔案,tail -f filename會把filename裡最尾部的內容顯示在螢幕上,並且不但重新整理,使你看到最新的檔案內容. 1.命令格式; tail[必要引數][選擇引數][檔案] 2.命令功能