Linux find命令用法小結

2021-06-02 06:16:06 字數 4369 閱讀 2837

find是個使用頻率比較高的命令。常常用它在系統特定目錄下,查詢具有某種特徵【名字體別屬主許可權等】的檔案。

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

path:要查詢的目錄路徑。

~ 表示$home目錄
. 表示當前目錄
/ 表示根目錄
-print :表示將結果輸出到標準輸出

-exec :對匹配的檔案執行該引數所給出的shell命令。形式為 command  {} \; ,注意{}與\; 之間有空格

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

options常用的有下選項:-name 按照名字查詢
-perm 安裝許可權查詢
-prune 不再當前指定的目錄下查詢
-user 檔案屬主來查詢
-group 所屬組來查詢
-nogroup 查詢無有效所屬組的檔案
-nouser 查詢無有效屬主的檔案
-type 按照檔案型別查詢

下面通過一些簡單的例子來介紹下find的常規用法:

1、按名字查詢

在當前目錄及子目錄中,查詢大寫字母開頭的txt檔案

[root@localhost ~]# find . -name '[a-z]*.txt' -print

在/etc及其子目錄中,查詢host開頭的檔案

[root@localhost ~]# find /etc -name 'host*' -print

在$home目錄及其子目錄中,查詢所有檔案

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

[root@localhost .code]# find . -name "out*" -prune -o -name "*.txt" -print

2、按目錄查詢

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

[root@localhost .code]# find . -path "./aa" -prune -o -name "*.txt" -print

在當前目錄及除aa和bb之外的子目錄中查詢txt檔案

[root@localhost .code]# find . \( -path "./aa" -o -path "./bb" \) -prune -o -name "*.txt" -print

在當前目錄,不再子目錄中,查詢txt檔案

[root@localhost .code]# find .  ! -name "." -type d -prune -o -type f -name "*.txt" -print
3、按許可權查詢

在當前目錄及子目錄中,查詢屬主具有讀寫執行,其他具有讀執行許可權的檔案

[root@localhost ~]# find . -perm 755 -print

4、按型別查詢

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

[root@localhost .code]# find . -type l -print

5、按屬主及屬組

查詢屬主是www的檔案

[root@localhost .code]# find / -user www -type f -print

查詢屬主被刪除的檔案

[root@localhost .code]# find /  -nouser -type f -print

查詢屬組mysql的檔案

[root@localhost .code]# find /  -group mysql -type f  -print

查詢使用者組被刪掉的檔案

[root@localhost .code]# find /  -nogroup -type f  -print

6、按時間查詢

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

[root@localhost .code]# find . -mtime -2 -type f -print

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

[root@localhost .code]# find . -mtime +2 -type f -print

查詢一天內被訪問的檔案

[root@localhost .code]# find . -atime -1 -type f -print

查詢一天前被訪問的檔案

[root@localhost .code]# find . -atime +1 -type f -print

查詢一天內狀態被改變的檔案

[root@localhost .code]# find . -ctime -1 -type f -print

查詢一天前狀態被改變的檔案

[root@localhost .code]# find . -ctime +1 -type f -print

查詢10分鐘以前狀態被改變的檔案

[root@localhost .code]# find . -cmin +10 -type f -print

7、按檔案新舊

查詢比aa.txt新的檔案

[root@localhost .code]# find . -newer "aa.txt"  -type f -print

查詢比aa.txt舊的檔案

[root@localhost .code]# find . ! -newer "aa.txt"  -type f -print

查詢比aa.txt新,比bb.txt舊的檔案

[root@localhost .code]# find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print

8、按大小查詢

查詢超過1m的檔案

[root@localhost .code]# find / -size +1m -type f -print

查詢等於6位元組的檔案

[root@localhost .code]# find . -size 6c -print

查詢小於32k的檔案

[root@localhost .code]# find . -size -32k -print

9、執行命令

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

[root@localhost .code]# find . -name 'del.txt' -ok rm {} \;

查詢aa.txt 並備份為aa.txt.bak

[root@localhost .code]# find . -name 'aa.txt' -exec cp {} {}.bak \;

查詢aa.txt 歸檔壓縮為aa.txt.tar.gz 並刪除aa.txt

find . -name "aa.txt" -type f -exec tar -zcvf {}.tar.gz {} \; -exec rm -rf {} \; > /dev/null

Linux find命令用法小結

find是個使用頻率比較高的命令。常常用它在系統特定目錄下,查詢具有某種特徵 名字體別屬主許可權等 的檔案。find命令的格式 find path options print exec ok path 要查詢的目錄路徑。表示 home目錄 表示當前目錄 表示根目錄 print 表示將結果輸出到標準輸...

Linux find命令小結

find 命令 今天瀏覽了下linux下find 的man文件,涉及的細節很多,僅把常用的部分整理出來,供後續豐富補充吧。find命令格式 usage find path expression path預設為當前路徑,expression預設為 print項 expressions有以下幾種組成 o...

linux find命令用法

find maxdept 1 name test.txt 查詢當前目錄下第一級目錄的test.txt檔案 type 指定檔案型別查詢 f 代表檔案 type f name txt 查詢 txt的檔案 mtime 時間 後面加時間表示多久以前修改的,加在最後 mtime 時間 代表幾天之內修改的 ex...