find 指令的基本資訊如下:
- 指令名稱:find。
- 英文原意:search for files in a directory hierarchy.
- 所在路徑:/bin/find。
- 執行權限:所有使用者。
- 功能描述:在目錄中查找檔案。
指令格式
find 搜尋路徑 [選項] 搜尋内容
find 是比較特殊的指令,它有兩個參數:
- 第一個參數用來指定搜尋路徑;
- 第二個參數用來指定搜尋内容。
1.按名稱或正規表達式查找檔案
[root@localhost ~]#find 搜尋路徑 [選項] 搜尋内容
選項:
- -name: 按照檔案名搜尋;
- -iname: 按照檔案名搜尋,不區分檔案名大小;
- -inum: 按照 inode 号搜尋;
按特定名稱搜尋檔案
find . -name test.txt
如何查找所有格式為 pdf 的書籍?使用正規表達式:
find . -name "*.pdf"
2.查找不同類型的檔案
[root@localhost ~]# find 搜尋路徑 [選項] 搜尋内容
選項:
- -type d:查找目錄
- -type f:查找普通檔案
- -type l:查找軟連結檔案
搜尋目錄:
find . -type d -name "apps*"
搜尋符号連接配接
find . -type l -name "apps*"
3.按指定的時間戳查找檔案
Linux 中的檔案有通路時間(atime)、資料修改時間(mtime)、狀态修改時間(ctime)這三個時間,我們也可以按照時間來搜尋檔案。
[root@localhost ~]# find搜尋路徑 [選項] 搜尋内容
選項:
- -atime [+-]時間: 按照檔案通路時間搜尋
- -mtime [+-]時間: 按照文改時間搜尋
- -ctime [+-]時間: 按照檔案修改時間搜尋
這裡用 mtime 資料修改時間來舉例,重點說說 "[+-]"時間的含義。
- -5:代表@内修改的檔案。
- 5:代表前5~6天那一天修改的檔案。
- +5:代表6天前修改的檔案
要搜尋 atime 超過5天的檔案
find . -type f -atime +5
如果我們需要查找 mtime 正好是 5 天前的檔案,請不要包含 +,因為它的意思是“大于”
find . -type f -mtime 5
搜尋 ctime 在 5~10 天前的檔案:
find . -type f -ctime +5 -ctime -10
4.按照檔案大小搜尋
[root@localhost ~]#find 搜尋路徑 [選項] 搜尋内容
選項:
- -size[+-]大小:按照指定大小搜尋檔案
這裡的"+"的意思是搜尋比指定大小還要大的檔案,"-" 的意思是搜尋比指定大小還要小的檔案。
find 指令的預設機關不是位元組。如果不寫機關,那麼 find 指令是按照 512 Byte 來進行査找的。 其計量機關指定為以下約定:
b:512 位元組塊(預設)
c:位元組
w:雙位元組字
k:KB
M:MB
G:GB
要查找大小為 10 MB ~ 1 GB 的檔案
find . -type f -size +10M -size -1G
5.按權限查找檔案
[root@localhost ~]# find 搜尋路徑 [選項] 搜尋内容
選項:
- -perm 權限模式:査找檔案權限剛好等于"權限模式"的檔案
- -perm -權限模式:査找檔案權限全部包含"權限模式"的檔案
- -perm +權限模式:査找檔案權限包含"權限模式"的任意一個權限的檔案
為了便于了解, 先建立幾個測試檔案,然後舉例說明。
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# touch testl
[root@localhost test]# touch test2
[root@localhost test]# touch test3
[root@localhost test]# touch test4
#建立測試目錄,以及測試檔案
[root@localhost test]# chmod 755 testl
[root@localhost test]# chmod 444 test2
[root@localhost test]# chmod 600 test3
[root@localhost test]# chmod 200 test4
#設定實驗權限。因為是實驗權限,是以看起來比較别扭
[root@localhost test]# ll
總用量0
-rwxr-xr-x 1 root root 0 6月 17 11:05 testl
-r--r--r-- 1 root root 0 6月 17 11:05 test2
-rw------- 1 root root 0 6月 17 11:05 test3
-w-------- 1 root root 0 6月 17 11:05 test4
#檢視權限
"-perm權限模式",代表査找的權限必須和指定的權限模式一模一樣
[root@localhost test]#find.-perm 444
./test2
[root@localhost test]#find.-perm 200
./test4
#按照指定權限搜尋檔案,檔案的權限必須和搜尋指定的權限一緻,才能找到
"-perm-權限模式",代表的是檔案的權限必須全部包含搜尋指令指定的權限模式,才可以找到
[root@localhost test]#find .-perm -200
./test4 <-此檔案權限為200
./test3 <-此檔案權限為600
./testl <-此檔案權限為755
#搜尋檔案的權限包含200的檔案,不會找到test2檔案,因為test2的權限為444,不包含200權限
因為 test4 的權限 200(-w-------)、test3 的權限 600(-rw-------)和 test1 的權限 755(-rwxr-xr-x) 都包含 200(--w-------) 權限,是以可以找到;而 test2 的權限是 444 (-r--r--r--),不包含 200 (--w-------)權限,是以找不到,
[root@localhost test]# find .-perm -444
.
./test2 <-此檔案權限為444
./test1 <-此檔案權限為755
#搜尋檔案的權限包含444的檔案
"-perm+權限模式",是隻要包含任意一個指定權限,就可以找到
[root@localhost test]# find .-perm +200
./test4 <-此檔案權限為200
./test3 <-此檔案權限為600
./testl <-此檔案權限為755
#搜尋檔案的權限包含200的檔案,不會找到test2檔案,因為test2的權限為444,不包含200權限。
因為 test4 的權限 200 (--w-------)、test3 的權限 600 (-rw-------)和 test1 的權限 755 (-rwxr-xr-x)都包含 200(--w-------)權限,是以可以找到;而 test2 的權限是 444 (-r--r--r--),不包含 200 (--w-------)權限,是以找不到。
6.按所有權查找檔案
[root@localhost ~]# find 搜尋路徑 [選項] 搜尋内容
選項:
- -uid 使用者 ID:按照使用者 ID 査找所有者是指定 ID 的檔案
- -gid 組 ID:按照使用者組 ID 査找所屬組是指定 ID 的檔案
- -user 使用者名:按照使用者名査找所有者是指定使用者的檔案
- -group 組名:按照組名査找所屬組是指定使用者組的檔案
- -nouser:査找沒有所有者的檔案
查找所有屬于 jenkins 的檔案
find -type f -user jenkins
7.find指令的集合運算
[root@localhost ~]#find 搜尋路徑 [選項] 搜尋内容
選項
- -a:and邏輯與(預設)
- -o:or邏輯或
- -not:not邏輯非
-a:and邏輯與
[root@localhost ~]# find.-size +2k -a -type f
#在目前目錄下搜尋大于2KB,并且檔案類型是普通檔案的檔案
-o:or邏輯或
[root@localhost ~]#find.-name jenkins -o -name tomcat
./jenkins
./tomcat
#在目前目錄下搜尋檔案名要麼是jenkins的檔案,要麼是tomcat的檔案
-not:not邏輯非
[root@localhost ~]# find.-not -name cangls
#在目前目錄下搜尋檔案名不是cangls的檔案
8.在找到檔案後執行指令
-exec選項
[root@localhost ~]# find 搜尋路徑 [選項] 搜尋内容 -exec 指令2{}\;
首先,請大家注意這裡的"{}"和"\;"是标準格式,隻要執行"-exec"選項,這兩個符号必須完整輸入,
-exec
選項後面的指令必須以分号(;)結束。
[root@localhost test]# find.-perm 444 -exec ls -l {}\;
-r--r--r-- 1 root root 0 6月 17 11:05 ./test2
#使用"-exec"選項,把find指令的結果直接交給"ls -l"指令處理
[root@localhost test]# find .-perm 444 -ok rm -rf{}\;
<rm…./test2>?y <-需要使用者輸入y,才會執行
#我們這次使用rm指令來删除find找到的結果,删除的動作最好确認一下