Linux下find命令詳解

2021-07-22 07:35:35 字數 3742 閱讀 6439

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。linux下find命令提供了相當多的查詢條件,功能很強大。

linux中find常見用法示例·

find path -option [ -print ] [ -exec -ok command ] {} \;

#-print 將查詢到的檔案輸出到標準輸出

#-exec command {} \; -----將查到的檔案執行command操作,{} 和 \;之間有空格

#-ok 和-exec相同,只不過在操作前要詢使用者

-name filename #查詢名為filename的檔案

-perm #按執行許可權來查詢

-user username #按檔案屬主來查詢

-group groupname #按組來查詢

-mtime -n +n #按檔案更改時間來查詢檔案,-n指n天以內,+n指n天以前

-atime -n +n #按檔案訪問時間來查gin: 0px">-perm #按執行許可權來查詢

-user username #按檔案屬主來查詢

-group groupname #按組來查詢

-mtime -n +n #按檔案更改時間來查詢檔案,-n指n天以內,+n指n天以前

-atime -n +n #按檔案訪問時間來查詢檔案,-n指n天以內,+n指n天以前

-ctime -n +n #按檔案建立時間來查詢檔案,-n指n天以內,+n指n天以前

-nogroup #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在

-nouser #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存

-newer f1 !f2 找檔案,-n指n天以內,+n指n天以前

-ctime -n +n #按檔案建立時間來查詢檔案,-n指n天以內,+n指n天以前

-nogroup #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在

-nouser #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存

-newer f1 !f2 #查更改時間比f1新但比f2舊的檔案

-type b/d/c/p/l/f #查是塊裝置、目錄、字元裝置、管道、符號鏈結、普通檔案

-size n[c] #查長度為n塊[或n位元組]的檔案

-depth #使查詢在進入子目錄前先行查詢完本目錄

-fstype #查更改時間比f1新但比f2舊的檔案

-mount #查檔案時不跨越檔案系統mount點

-follow #如果遇到符號鏈結檔案,就跟蹤鏈結所指的檔案

-cpio #對匹配的檔案使用cpio命令,將他們備份到磁帶裝置中

-prune #忽略某個目錄

更多資訊可以檢視:《linux下find命令詳解》下find命令例項》

1)在/tmp中查詢所有的*.h,並在這些檔案中查詢「syscall_vector",最後列印出所有包含"syscall_vector"的檔名

a) find /tmp -name "*.h" | xargs -n50 grep syscall_vector

b) grep syscall_vector /tmp/*.h | cut -d':' -f1| uniq > filename

c) find /tmp -name "*.h" -exec grep "syscall_vector" {} \; -print

2)find / -name filename -exec rm -rf {} \;

find / -name filename -ok rm -rf {} \;

3)比如要查詢磁碟中大於3m的檔案:

find . -size +3000k -exec ls -ld {} ;

4)將find出來的東西拷到另乙個地方

find *.c -exec cp '{}' /tmp ';'

如果有特殊檔案,可以用cpio,也可以用這樣的語法:

find dir -name filename -print | cpio -pdv newdir

6)查詢2004-11-30 16:36:37時更改過的檔案

# a=`find ./ -name "*php"` | ls -l --full-time $a 2>/dev/null | grep "2004-11-30 16:36:37

二、linux下find命令的用法1. 基本用法:

find / -name 檔名

find ver1.d ver2.d -name '*.c' -print 查詢ver1.d,ver2.d *.c檔案並列印

find . -type d -print 從當前目錄查詢,僅查詢目錄,找到後,列印路徑名。可用於列印目錄結構。

2. 無錯誤查詢:

find / -name access_log 2 >/dev/null

3. 按尺寸查詢:

find / -size 1500c (查詢1,500位元組大小的檔案,c表示位元組)

find / -size +1500c (查詢大於1,500位元組大小的檔案,+表示大於)

find / -size +1500c (查詢小於1,500位元組大小的檔案,-表示小於)

find / -amin n 最後n分鐘

find / -atime n 最後n天

find / -cmin n 最後n分鐘改變狀態

find / -ctime n 最後n天改變狀態

5. 其它:

find / -empty 空白檔案、空白資料夾、沒有子目錄的資料夾

find / -false 查詢系統中總是錯誤的檔案

find / -fstype type 找存在於指定檔案系統的檔案,如type為ext2

find / -gid n 組id為n的檔案

find / -group gname 組名為gname的檔案

find / -depth n 在某層指定目錄中優先查詢檔案內容

find / -maxdepth levels 在某個層次目錄中按遞減方式查詢

6. 邏輯

-and 條件與 -or 條件或

7. 查詢字串

find . -name '*.html' -exec grep 'mailto:'{}

linux下find命令詳解

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。linux下find命令提供了相當多的查詢條件,功能很強大。linux中find常見用法示例 find path option print exec ok command print 將查詢到的檔案輸出到標準輸出 exec comman...

Linux下find命令詳解

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。linux下find命令提供了相當多的查詢條件,功能很強大。linux中find常見用法示例 find path option print exec ok command print 將查詢到的檔案輸出到標準輸出 exec comman...

Linux下find命令詳解

功能 檔案查詢 命令原型 find path option print exec ok command 引數介紹 pathname find命令所查詢的目錄路徑。print find命令將匹配的檔案輸出到標準輸出 exec find命令對匹配的檔案執行該引數所給出的shell命令。相應命令的形式為 ...