linux find命令的總結

2021-07-29 15:05:07 字數 3937 閱讀 5875

linux下的find命令在目錄結構中搜尋檔案,並執行指定的操作。

linux下的find命令提供了相當多的查詢條件,功能很強大。由於find具有強大的功能,所以它的

選項特別特別多。

在執行乙個非常消耗資源的find命令時,很多人都傾向於把它放在後台執行,因為遍歷乙個大的文

件系統可能會花費很長的時間。

find命令的格式:find [-path...] -options[-print -exec -ok]

path:要查詢的目錄路徑。

~表示¥home目錄

.表示當前目錄

/表示根目錄

-print:表示講結果輸出到標準輸出

-exec:對匹配的檔案執行該引數所給出的shell命令。

形式為command {} \;  ({}和\;之間有空格)      

-ok:與-exec作用相同,區別在於,在執行命令之前,都會給出提示,讓使用者確認執行

option常用的選項:

-name按照名字查詢

-perm安裝許可權查詢

-prune不再當前指定的目錄下查詢

-user檔案屬主來查詢

-group所屬組來查詢

-type按照檔案型別來查詢

-nouser查詢無有效屬主的檔案

-nogroup查詢無有效的所屬組的檔案

我簡單的舉兩個例子:

一:find -name

擴充套件幾個-name用法

1.在當前目錄及子目錄中,查詢大寫字母

開頭的txt檔案

[liang@localhost code]$ find . -name '[a-z]*.txt' -print
2.在/etc及其子目錄中,查詢host開頭的檔案

[liang@localhost code]$ find /etc -name 'host*' -print
3.在$home目錄下及其子目錄中,查詢所有檔案

[liang@localhost ~]$ find ~ -name '*' -print
4.在當前目錄及子目錄中,查詢不是out開頭的txt檔案

[liang@localhost ~]$ find . -name "out*" -prune -o -name "*.txt" -print
二.按目錄查詢

1.在當前目錄除了aa之外的子目錄內搜尋txt檔案

[liang@localhost ~]$ find . -path "./aa" -prune -o -name "*.txt" -print
2.在當前目錄及除aa和bb之外的子目錄中查詢txt檔案

[liang@localhost ~]$ find . -path"./aa" -o-path "./bb" -prune -o -name "*.txt" -print
3.在當前目錄,不再子目錄中,查詢txt檔案

[liang@localhost ~]$ find . ! -name "." -type d -prune -o -type f -name "*.txt" -print
三.按許可權查詢

四.按型別查詢

在當前目錄及子目錄下,查詢符號鏈結檔案

[liang@localhost ~]$ find . -type l -print
五.按照屬主及屬組

查詢屬主是root的檔案:

2.查詢屬主被刪除的檔案

[liang@localhost ~]$ find / -nouser -type f -print
3.查詢屬組liang的檔案

[liang@localhost ~]$ find / -group liang -type f -print
4.查詢屬組被刪除的檔案

find / -nogroup -type f -print
六.按時間查詢

查詢2天內被更改過的檔案

[liang@localhost ~]$ find . -mtime -2 -type f -print
查詢兩天前被更改過得檔案

[liang@localhost ~]$ find . -mtime +2 -type f -print
查詢一天內被訪問的檔案

[liang@localhost ~]$ find . -atime -1 -type f -print
查詢一天前被訪問的檔案

[liang@localhost ~]$ find . -atime +1 -type f -print
查詢10分鐘以前狀態被改變的檔案

[liang@localhost ~]$ find . -cmin +10 -type f -print
七。按檔案新舊

1.查詢比aa.txt新的檔案

[liang@localhost ~]$ find . -newer "aa.txt"  -type f -print
2.查詢比aa.txt舊的檔案

[liang@localhost ~]$ find . ! -newer "aa.txt"  -type f -print
3.查詢比aa.txt新,比bb.txt舊的檔案

[liang@localhost ~]$ find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print
八.按大小查詢

1.查詢超過1m的檔案

[liang@localhost ~]$ find / -size +1m -type f -print
2.查詢等於100位元組的檔案

[liang@localhost ~]$  find . -size 100c -print
3.查詢小於50kb的檔案

[liang@localhost ~]$ find . -size -50k -print
九.執行命令 

1.查詢del.txt並刪除,刪除前提示確認

[liang@localhost ~]$ find . -name 'del.txt' -ok rm {} \;
2.查詢aa.txt並備份為aa.txt.bak

[liang@localhost ~]$ find . -name 'aa.txt' -exec cp {} {}.bak \;
3.查詢aa.txt歸檔壓縮為aa.txt.tar.gz並刪除aa.txt

[liang@localhost ~]$find . -name "aa.txt" -type f -exec tar -zcvf {}.tar.gz {} \; -exec rm -rf {} \; > /dev/null

Linux find 命令總結

查詢檔案是非常常見的系統操作,linux可以使用find命令來進行檔案查詢,用好find命令,會讓你感受到前所未有的痛快。find有眾多選項和引數,熟練運用它們,你才能真正感受到find命令的強大,總結如下。命令格式 find pathname option print exec ok comman...

linux find 命令總結

這裡總結一些常用到的關於find的命令的操作 b find命令的一般形式為 b find pathname options print exec ok 讓我們來看看該命令的引數 pathname find命令所查詢的目錄路徑。例如用.來表示當前目錄,用 來表示系統根目錄。print find命令將匹...

Linux Find 命令總結

三豐雲,免費虛擬主機和免費雲伺服器相當不錯,使用起來非常快,對於個人使用者來說足夠用了,有需要的朋友來看看吧,我已經使用過了 體驗很不錯的 1 按檔名遞迴查詢 find name filename 2 按檔名遞迴查詢,不區分大小寫 find iname filename 3 查詢當前目錄及深度為1的...