1. 程式人生 > 其它 >Linux檔案查詢-find

Linux檔案查詢-find

1. find-命令詳解;

概念:find命令用於查詢目錄下的檔案,同時也可以呼叫其他命令執行相應的操作。

應用場景:忘記檔案所在位置,或者需要通過內容查詢,這時就需要find命令了。

語法格式:find 路徑 選項 表示式 動作

查詢方式:檔名稱、檔案大小、檔案時間、檔案許可權、屬主屬主等。

2.find使用範例

2.1 基於名稱查詢;

查詢/etc目錄下包含ifcfg-eth名稱所有檔案,-i選項可以忽略大小寫。

2.2 基於檔案大小 查詢,

#1.查詢大於2M的檔案;

find /etc/ -size +2M

#2.查詢等於2M的檔案;

find /etc/ -size 2M

#3.查詢小於2M的檔案;

find /etc/ -size -2M

#4.查詢大於5M,但是小於7M的檔案;fing /etc/ -size +5M -a -size -7M | xargs ls -lh

2.3 基於型別查詢

#1. f=檔案

find /dev -type f

#2. d=目錄

find /dev -type d

#3. l=連結

find /dev -type l

#4. b=塊裝置

find /dev -type b

#5. c=字元裝置

find /dev -type c

#6. s=套接字

find /dev -type s

#7. p=管道檔案

find /dev -type p

2.4 基於時間查詢

#1. for i in {01..10};do date -s 20104$i && touch file-$i;done ==建立測試檔案;

#2. find ./ -iname "file-*" -mtime +7 ==查詢7天以前的檔案,(不會列印第7天和當天的檔案),如果刪除,第1天和第2天的檔案會被刪除,第3天到第9天的資料會儲存下來,相當於保留了最近7天的資料。

[root@localhost ~]# find ./ -iname "file-*" -mtime +7
./file-01
./file-02
#3. find ./ -iname "file-*" -mtime -7 ==查詢最近7天的檔案,不建議使用(會列印當天的檔案),最近7天,也就是第4天到第10天的資料,如果刪除,那就是刪除了第4天到第10天的資料。

[root@localhost ~]# find ./ -iname "file-*" -mtime -7
./file-05
./file-06
./file-08
./file-09
./file-04
./file-07
./file-10
#4. find ./ -iname "file_*" -mtime 7 ==查詢第7天檔案。

[root@localhost ~]# find ./ -iname "file-*" -mtime 7
./file-03
#5. find ./ -iname "file_*" -mmin 100 =按檔案的修改時間來查詢檔案(單位分鐘)。

[root@localhost ~]# find ./ -iname "file-*" -mmin +10
./file-05
./file-06
./file-08
./file-09
./file-01
./file-03
./file-04
./file-07
./file-10
./file-02
#6. 查詢/var/log下所有以.log結尾的檔案,並保留最近7天的log檔案。

[root@localhost ~]# find /var/log/ -type f -mtime -7 -a -name "*.log"
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log

2.5 基於使用者查詢

#1. 查詢屬主是jack

find /home -user jack

#2. 查詢屬組是admin

find /home -group admin

#3. 查詢屬主是jack,屬組是admin

find /home -user jack -group admin

#4. 查詢屬主是jack,並且屬組是admin

find /home -user jack -a -group admin

#5. 查詢屬主是jack,或者屬組是admin

find /home -user jack -o -group admin

#6. 查詢沒有屬主

find /home -nouser

#7. 查詢沒有屬組

find /home -nogroup

#8. 查詢沒有屬主,或者沒有屬組

find /home -nouser -o -nogroup

2.6 基於許可權查詢

-perm [/ | -]MODE

MODE: 精確匹配,屬主、屬組、其他使用者,每一項的許可權必須相等。

find /root -type f -perm 644 -ls

-MODE: 每一項許可權必須包含所指定的許可權,例:包含(u涵蓋6,並且g涵蓋4,並且o涵蓋4)。

find /root -type f -perm -644 -ls

/MODE: /或者(U為6 或者g為4 或者o為0),三項只要有一項匹配即可。

find /root -type f -perm -644 -ls

特殊許可權(最低許可權是4000,4755也滿足需求)

find /usr/bin/ /usr/sbin/ -type f -perm -4000 -ls

find /usr/bin/ /usr/sbin/ -type f -perm -2000 -ls

find /usr/bin/ /usr/sbin/ -type f -perm -1000 -ls

2.7 邏輯運算子

-a 與(並且)
-o 或(或者)
-not|! 非(取反)

#1.查詢當前目錄下,屬主不是root的所有檔案

find ./ ! -user root

#2.查詢當前目錄下,屬主屬於hdfs,並且大小大於1k的檔案

find ./ -type f -a -user hdfs -a -size +1k

#3.查詢當前目錄下的屬主為root或者以xml結尾的普通檔案

find ./ type f -a \(-user root -o -name "*.xml"\) 

3.find動作處理

查詢到一個檔案後,需要對檔案進行如何處理 find的預設動作是 -print

-print 列印查詢到的內容(預設)
-ls 以長格式顯示的方式列印查詢到的內容
-delete 刪除查詢到的檔案(僅能刪除空目錄)
-ok 後面跟自定義 shell 命令(會提示是否操作)
-exec 後面跟自定義 shell 命令(標準寫法 -exec \;)
-maxdepth 查詢一級目錄==tree -L 1

3.1find結合exec

find /etc -name"ifcfg*" -exec cp -rvf {} /tmp \; 複製

find /etc -name"ifcfg*" -exec rm -f {} \;

find /etc -name"ifcfg*" -exec mv {} /tmp \;

3.2 find結合xargs

xargs 將前者命令查詢到的檔案作為一個整體傳遞後者命令的輸入,所以其操作的額效能極高;
exec 是將檔案一個一個的處理,所以處理效能極低;

#1.刪除檔案,效能對比。

touch file-{1..10000}

find ./ -name "file-*" -exec rm -f {} \;

find ./ -name "file-*" | xargs rm -f

#2.檔案拷貝

find /usr/sbin/ -type f -perm -4000 | xargs -i cp -rv {} /tmp

find /usr/sbin/ -type f -perm -4000 | xargs -I {} cp -rv {} /tmp

#3.find結合grep

當忘記重要配置檔案儲存路徑時,可通過搜尋關鍵字獲取檔案其路徑;

find /code -type f |xargs grep -R "MySQL_PASSWORD"

find /etc/ -type f |xargs grep -R "oldxu.com"