shell命令 find查詢命令

2021-09-03 01:49:41 字數 2229 閱讀 5529

一般用法:

find 目錄 引數  #按要求列出指定目錄及其子目錄下的所有檔案和目錄,指定目錄要在引數之前
限定查詢檔案型別:

find

. -type d #只列出目錄

find

. -type f #只列出檔案

限定查詢目錄的深度,僅查詢當前目錄而不查詢子目錄深度用1:

find

. -maxdepth 1 #只查詢當前目錄

find

. -mindepth 2 #只查詢子目錄

根據檔名(或目錄名)查詢:

find

. -name "*.txt"

#查詢所有名稱為*.txt的檔案或目錄

find

. -iname "*.pdf"

#忽略大小寫查詢所有名稱為*.txt的檔案或目錄

find

. \( -name "*.txt" -o -name "*.pdf" \)

#or條件,匹配多個條件中的乙個。注意括號用法,注意空格:\( 和 \)。

根據路徑名和檔名(或目錄名)查詢:

find /root -path "/root/t/*.txt"

find /root -ipath "/root/t/*.txt"

#忽略大小寫

基於正規表示式的路徑名和檔名(或目錄名)查詢:

find /root -regex "/root/t/.*scala"

find /root -iregex "/root/t/.*s$"

#忽略大小寫

否定引數!:

find

.! -name "*.txt"

#查詢所有名稱不為*.txt的檔案或目錄

根據檔案時間來查詢(最後訪問時間-atime,最後修改時間-mtime,檔案元資料(例如許可權或所有權)最後變化時間-ctime),+表示大於,-表示小於,單位為天:

find

. -type f -atime -7 #列出最近7天訪問過的檔案

find

. -type f -atime 7 #列出恰好7天前訪問過的檔案

find

. -type f -atime +7 #列出訪問時間超過7天的檔案

#以下是基於分鐘的引數-amin,-mmin,-cmin

find

. -type f -amin +3 #列出訪問時間超過3分鐘的檔案

時間比較引數-newer:

find

.type f -newer this.txt #列出比this.txt更新的(更長的修改時間)所有檔案

基於檔案大小查詢-size:

find

. -type f -size +2k #列出大於2kb的檔案

find

. -type f -size -2m #列出小於2mb的檔案

find

. -type f -size 2g #列出2gb的檔案

find

. -type f -size +2c #列出大於2b的檔案

##若計數值是1的時候不準確(如1k),所以轉換單位(比如轉為1024c)

-delete選項,刪除查詢到的所有檔案:

find

. -type f -name "*.txt" -delete #刪除所有txt檔案

-exec選項,利用find的結果執行其他命令:

find

. -type f -name "*.txt" -exec cat

\;> /root/all.txt #將所有txt檔案合併到all.txt裡,注意輸出檔案最好不要是被查詢到的檔案之一

find

. -type f -name "*.txt" -exec ls -hl \;

#用ls列出所有查到的檔案的資訊

列出當前目錄下所有檔案(和目錄),不包括abc目錄下的(和abc目錄本身):

find

. \( -name "abc" -prune \) -o \( -print \)

find 查詢命令

find 按條件查詢檔案 根據預設的條件遞迴查詢對應的檔案 find 目錄 條件1 a o 條件2 systemctl restart chronyd systemctl enable chronyd 常用條件表示 type 按文件型別查詢,檔案 f 目錄 d 裝置 b,c 快捷方式 鏈結 i na...

查詢命令find

find option 查詢路徑 查詢條件 處理動作 查詢路徑 指定具體目標路徑 預設為當前目錄 查詢條件 指定的查詢標準,可以檔名 大小 型別 許可權等標準進行 預設為找出指定路徑下的所有檔案 處理動作 對符合條件的檔案做操作,預設輸出至螢幕 查詢條件 name 檔名稱 支援使用glob inam...

shell指令碼 find命令

find命令用於查詢,功能很強大,格式如下 find pathname options print exec ok pathname find命令所查詢的目錄路徑。例如用.來表示當前目錄,用 來表示系統根目錄。print find命令將匹配的檔案輸出到標準輸出。exec find命令對匹配的檔案執行...