Find 過濾搜索、目錄層級限制(-maxdepth、-mindepth)以及常用搜索技巧小結


 

1)find過濾目錄
使用find命令在linux系統中查找文件時,有時需要忽略某些目錄,可以使用"-path 過濾的目錄路徑 -prune -o"參數來進行過濾。不過必須注意:要忽略的路徑參數要緊跟着搜索的路徑之后,否則該參數無法起作用。

首先拿一個例子來說明下:
比如查找/data/web/ssy/online路徑下的的目錄,並統計目錄大小,以G位單位進行排序(默認為降序),並統計前10個大小的目錄。命令如下:
# find /data/web/ssy/online/* -maxdepth 0 -type d -exec /usr/bin/du -sh {} \;|grep '[0-9]G'|sort -rh|head -10

查找/data/web/ssy/online路徑下除tmp目錄之外的目錄,並統計目錄大小,以G位單位進行排序(默認為降序),並統計前10個大小的目錄。命令如下
# find /data/web/ssy/online/* -path /data/web/ssy/online/tmp -prune -o -maxdepth 0 -type d -exec /usr/bin/du -sh {} \;|grep '[0-9]G'|sort -rh|head -10

注意:
1)"-maxdepth 0" 表示只查找到/data/web/ssy/online下的目錄。如果是"-maxdepth 1"則表示查找到/data/web/ssy/online/xxx下的目錄
2)find命令中的過濾、忽略、排除使用"-path 過濾的文件或目錄-prune -o ",其中-prune類似於if判斷,如果-prune之前的語句為真,比如找到了
   前面-path指定的/data/web/ssy/online/tmp目錄,就不再執行后面-o跟的語句了,如果沒有找到則執行后面的語句。這樣就做到了排除效果!
   其中的"-o" 是 "-or" 的意思!
3)-path要過濾掉的文件或目錄路徑參數一定要緊跟在要搜索的路徑之后,否則過濾效果就不會實現!!也就是說上面的"-path /data/web/ssy/online/tmp"
   必須緊跟着放在"/data/web/ssy/online/*"后面,否則查找時就不會過來掉/data/web/ssy/online/tmp這個目錄。

========================================================================================================================================================
示例一:
假設/opt/kevin目錄下有三個目錄:test1,test2,test3,三個目錄下都有list文件
[root@localhost kevin]# pwd
/opt/kevin
[root@localhost kevin]# ls
test1  test2  test3

現在要查找/opt/kevin路徑下的list文件,並忽略掉test2目錄,操作如下:
[root@localhost kevin]# pwd
/opt/kevin
[root@localhost kevin]# find . -type f -name list
./test1/list
./test2/list
./test3/list
[root@localhost kevin]# find . -type f -name list -print
./test1/list
./test2/list
./test3/list

使用-path 和 -prune -o實現過濾效果
[root@localhost kevin]# find . -path test2 -prune -o -type f -name list -print   
./test1/list
./test2/list
./test3/list

[root@localhost kevin]# find . -path ./test2/ -prune -o -type f -name list -print
find: warning: -path ./test2/ will not match anything because it ends with /.
./test1/list
./test2/list
./test3/list

當搜索路徑不是全路徑時,過濾目錄路徑必須是./test2 才能實現過濾效果!
[root@localhost kevin]# find . -path ./test2 -prune -o -type f -name list -print
./test1/list
./test3/list


要過濾的目錄操作-path必須緊跟着搜索路徑 才能實現過濾效果
[root@localhost kevin]# find . -type f -path ./test2 -prune -o -name list -print 
./test1/list
./test2/list
./test3/list

當搜索路徑時全路徑時,過濾路徑也要是全路徑,才能實現過濾效果
[root@localhost kevin]# find . -path /opt/kevin/test2 -prune -o -type f -name list -print   
./test1/list
./test2/list
./test3/list

[root@localhost kevin]# find /opt/kevin/ -path /opt/kevin/test2 -prune -o -type f -name list -print
/opt/kevin/test1/list
/opt/kevin/test3/list

[root@localhost kevin]# find /opt/kevin/* -path /opt/kevin/test2 -prune -o -type f -name list -print
/opt/kevin/test1/list
/opt/kevin/test3/list

[root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2 -prune -o -type f -name list -print  
/opt/kevin/test1/list
/opt/kevin/test3/list

[root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2/ -prune -o -type f -name list -print
find: warning: -path /opt/kevin/test2/ will not match anything because it ends with /.
/opt/kevin/test1/list
/opt/kevin/test2/list
/opt/kevin/test3/list

由上面可知:
1)當要搜索的目錄不是全路徑時,要過濾掉的目錄必須是"./test2"才能實現過濾效果。如果是"test2"或者"./test2/"都不能實現過濾效果。
2)當要搜索的目錄是全路徑時,要過濾掉的目錄也必須是全路徑才能實現過濾效果!要過濾掉的目錄后面不能加"/",否則也不能實現過濾效果。
3)過濾操作"-path /opt/kevin/test2/ -prune -o"必須緊跟在要搜索路徑的后面才能實現過濾效果,否則也不能實現過濾效果。

如果要過濾兩個目錄,比如過濾掉test2和test3目錄,則使用轉義符\( -path ./test2 -o -path ./test3 -prune -o \)
注意:兩個轉義符前面都要有空格!!

[root@localhost kevin]# find . -path ./test2 -o -path ./test3 -prune -o -type f -name list -print
./test1/list
./test2/list

[root@localhost kevin]# find . \( -path ./test2 -o -path ./test3 \) -prune -o -type f -name list -print
./test1/list

[root@localhost kevin]# find /opt/kevin/ \( -path /opt/kevin/test2 -o -path /opt/kevin/test3 \) -prune -o -type f -name list -print   
/opt/kevin/test1/list

除了上面的方法,還有一個方法如下:
[root@localhost kevin]# find . -type f -name list ! -path ./test2/* ! -path ./test3/*      
./test1/list

2)find過濾文件
先查看對應文件,然后使用"grep -v"進行過濾

比如只查找/opt/kevin目錄下的文件(不查找/opt/kevin的二級目錄下的文件),並過濾到haha2文件
[root@localhost kevin]# pwd
/opt/kevin
[root@localhost kevin]# ll
total 0
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha1
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha2
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha3
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha4
drwxr-xr-x 2 root root 18 Nov 21 18:24 test1
drwxr-xr-x 2 root root 18 Nov 21 18:24 test2
drwxr-xr-x 2 root root 18 Nov 21 18:24 test3

[root@localhost kevin]# find . -maxdepth 1 -type f 
./haha
./haha1
./haha2
./haha3
./haha4

[root@localhost kevin]# find . -maxdepth 1 -type f |grep -v "haha2"
./haha
./haha1
./haha3
./haha4

過濾多個文件,就使用多個"grep -v"
[root@localhost kevin]# find . -maxdepth 1 -type f |grep -v "haha2"
./haha
./haha1
./haha3
./haha4

[root@localhost kevin]# find . -maxdepth 1 -type f |grep -v "haha2"|grep -v haha3
./haha
./haha1
./haha4

[root@localhost kevin]# find . -maxdepth 1 -type f |grep -v "haha2"|grep -v haha3|grep -v haha4
./haha
./haha1

3)find命令中的-maxdepth和-mindepth:控制搜索深度的選項
-maxdepth :指定遍歷搜索的最大深度。最大目錄層級
-mindepth: 指定開始遍歷搜索的最小深度。最小目錄層級

-maxdepth 0:最大目錄層級為0,表示只針對當前目錄本身(比如/opt/kevin)進行搜索操作或du -sh 統計操作。
-maxdepth 1:最大目錄層級為1,表示針對/opt/kevin/ 路徑進行搜索操作或du -sh 統計操作。
-maxdepth 2:最大目錄層級為2,表示針對/opt/kevin/xxx/ 路徑進行搜索操作或du -sh 統計操作。
  
[root@localhost kevin]# pwd
/opt/kevin
[root@localhost kevin]# ll
total 0
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha1
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha2
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha3
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha4
drwxr-xr-x 2 root root 18 Nov 21 18:24 test1
drwxr-xr-x 2 root root 18 Nov 21 18:24 test2
drwxr-xr-x 2 root root 18 Nov 21 18:24 test3
  
-maxdepth 0 表示最小目錄層級是0,即搜索路徑它本身
[root@localhost kevin]# find . -maxdepth 0 -type f
 
但是如果當前路徑加入"*"使用"-maxdepth 0" 效果和 當前路徑不加"*"使用"-maxdepth 1" 是一樣的!
[root@localhost kevin]# find ./* -maxdepth 0 -type f
./haha
./haha1
./haha2
./haha3
./haha4
  
[root@localhost kevin]# find . -maxdepth 1 -type f
./haha
./haha1
./haha2
./haha3
./haha4
  
[root@localhost kevin]# find /opt/kevin -maxdepth 0 -type f  
[root@localhost kevin]# find /opt/kevin/ -maxdepth 0 -type f
[root@localhost kevin]# find /opt/kevin/* -maxdepth 0 -type f
/opt/kevin/haha
/opt/kevin/haha1
/opt/kevin/haha2
/opt/kevin/haha3
 
[root@localhost kevin]# find /opt/kevin -maxdepth 1 -type f 
/opt/kevin/haha
/opt/kevin/haha1
/opt/kevin/haha2
/opt/kevin/haha3
/opt/kevin/haha4
[root@localhost kevin]# find /opt/kevin/ -maxdepth 1 -type f
/opt/kevin/haha
/opt/kevin/haha1
/opt/kevin/haha2
/opt/kevin/haha3
/opt/kevin/haha4
 
[root@localhost kevin]# find /opt/kevin/* -maxdepth 1 -type f
/opt/kevin/haha
/opt/kevin/haha1
/opt/kevin/haha2
/opt/kevin/haha3
/opt/kevin/haha4
/opt/kevin/test1/list
/opt/kevin/test2/list
/opt/kevin/test3/list
 
[root@localhost kevin]# find . -maxdepth 2 -type f
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
 
結論:
如果搜索路徑后面加了"*",則使用"-maxdepth n"
和
不加"*"使用"-maxdepth n+1"
的效果是一樣的!!
  
超過了實際目錄級層,效果是一樣的
[root@localhost kevin]# find . -maxdepth 3 -type f
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
  
[root@localhost kevin]# find . -maxdepth 4 -type f
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
  
如果僅僅只是在/opt/kevin/xxx下搜索,即這里的最小目錄深度是2
[root@localhost kevin]# pwd
/opt/kevin
[root@localhost kevin]# ll
total 0
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha1
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha2
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha3
-rw-r--r-- 1 root root  0 Nov 21 18:51 haha4
drwxr-xr-x 2 root root 18 Nov 21 18:24 test1
drwxr-xr-x 2 root root 18 Nov 21 18:24 test2
drwxr-xr-x 2 root root 18 Nov 21 18:24 test3
  
[root@localhost kevin]# find . -mindepth 2 -type f
./test1/list
./test2/list
./test3/list
  
最小目錄層級為0
[root@localhost kevin]# find . -mindepth 0 -type f
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
  
最小目錄層級為1
[root@localhost kevin]# find . -mindepth 1 -type f
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
  
最小目錄層級為3,即超過當前最大目錄層級,則就搜索不到了!
[root@localhost kevin]# find . -mindepth 3 -type f

========================================================================
-mindepth和-maxdepth可以一起結合起來使用,用於搜索指定層級范圍內的文件。
========================================================================
如果只想搜索/opt/kevin/xxx下的文件,可行的做法:
第一種做法:最大目錄層級是1,即-maxdepth 1
[root@localhost kevin]# find ./* -maxdepth 0 -type f 
./haha
./haha1
./haha2
./haha3
./haha4

[root@localhost kevin]# find . -maxdepth 1 -type f             
./haha
./haha1
./haha2
./haha3
./haha4

第二種做法:最小目錄層級是1,最大目錄層級是1,即-mindepth 1 -maxdepth 1
[root@localhost kevin]# find . -mindepth 1 -maxdepth 1 -type f  
./haha
./haha1
./haha2
./haha3
./haha4

再來看下面的示例
[root@localhost kevin]# echo "123456" > bo/bobo/list1
[root@localhost kevin]# echo "123456" > bo/bobo/list2
[root@localhost kevin]# echo "123456" > bo/bobo/ke/list3
[root@localhost kevin]# echo "123456" > bo/bobo/ke/list4
[root@localhost kevin]# ll bo/
total 0
drwxr-xr-x 3 root root 42 Nov 21 23:23 bobo
[root@localhost kevin]# ll bo/bobo/
total 8
drwxr-xr-x 2 root root 32 Nov 21 23:23 ke
-rw-r--r-- 1 root root  7 Nov 21 23:23 list1
-rw-r--r-- 1 root root  7 Nov 21 23:23 list2
[root@localhost kevin]# ll bo/bobo/ke/
total 8
-rw-r--r-- 1 root root 7 Nov 21 23:23 list3
-rw-r--r-- 1 root root 7 Nov 21 23:23 list4

如果想搜索/opt/kevin/xxx/xxx下的文件,即最小目錄層級是3,最大目錄層級是3
[root@localhost kevin]# find . -mindepth 3 -type f            
./bo/bobo/ke/list3
./bo/bobo/ke/list4
./bo/bobo/list1
./bo/bobo/list2

[root@localhost kevin]# find . -maxdepth 3 -type f            
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
./bo/bobo/list1
./bo/bobo/list2

[root@localhost kevin]# find . -mindepth 3 -maxdepth 3 -type f
./bo/bobo/list1
./bo/bobo/list2

如果想要搜索第二層級和第三層級之間的文件,如下:
[root@localhost kevin]# find . -mindepth 2 -type f            
./test1/list
./test2/list
./test3/list
./bo/bobo/ke/list3
./bo/bobo/ke/list4
./bo/bobo/list1
./bo/bobo/list2

[root@localhost kevin]# find . -maxdepth 3 -type f              
./test1/list
./test2/list
./test3/list
./haha
./haha1
./haha2
./haha3
./haha4
./bo/bobo/list1
./bo/bobo/list2

[root@localhost kevin]# find . -mindepth 2 -maxdepth 3 -type f 
./test1/list
./test2/list
./test3/list
./bo/bobo/list1
./bo/bobo/list2

############  更多小示例說明  ############

1. 在當前目錄下查找所有txt后綴文件
# find ./ -name "*.txt"

2.在當前目錄下的dir0目錄及子目錄下查找txt后綴文件
# find ./ -path "./dir0*" -name "*.txt" 

3.在當前目錄下的dir0目錄下的子目錄dir00及其子目錄下查找txt后綴文件
# find ./ -path "*dir00*" -name "*.txt"

4.在除dir0及子目錄以外的目錄下查找txt后綴文件
# find ./ -path "./dir0*" -a -prune -o -name "*.txt" -print
這里注意:
-a 是and的縮寫, 意思是邏輯運算符'與'(&&); 
-o 是or的縮寫, 意思是邏輯運算符'或'(||), - not 表示非.
上條命令的意思是:
如果目錄dir0存在(即-a左邊為真),則求-prune的值,-prune 返回真,'與'邏輯表達式為真(即-path './dir0*' -a -prune 為真),
find命令將在除這個目錄以外的目錄下查找txt后綴文件並打印出來;
如果目錄dir0不存在(即-a左邊為假),則不求值-prune ,'與'邏輯表達式為假,則在當前目錄下查找所有txt后綴文件。

5.在dir0、dir1及子目錄下查找txt后綴文件
# find ./ \( -path "./dir0*" -o -path "./dir1*" \)  -a -name "*.txt" -print

6.在除dir0、dir1及子目錄以外的目錄下查找txt后綴文件
# find ./ \( -path "./dir0*" -o -path "./dir1*" \) -a -prune -o -name "*.txt" -print
這里注意:
圓括號()表示表達式的結合。即指示 shell 不對后面的字符作特殊解釋,而留給 find 命令去解釋其意義。由於命令行不能直接使用圓括號,
所以需要用反斜杠'\'進行轉意(即'\'轉意字符使命令行認識圓括號)。同時注意'\(','\)'兩邊都需空格。

7. 在所有以名為dir_general的目錄下查找txt后綴文件
# find ./ -path "*/dir_general/*" -name "*.txt" -print

8. 在當前目錄下,過濾.git目錄內容(.git目錄本身也不要列出來),命令如下:
# find . -path ./.git -prune -o -print -a \( -type f -o -type l -o -type d \) | grep '.git'

########## find文件搜索條件 ############
-name、-iname、通配符*?、-size、-user、-group、-amin、-cmin、-mmin、-a、-o、-exec/-ok、-inum

#find [搜索范圍] [匹配條件]
-------------------------------------------------
1. 根據文件名稱進行find檢索
find 命令中的 -name 選項可以根據文件名稱進行檢索(區分大小寫)。如需要忽略文件名中的大小寫,可以使用 -iname 選項。
-name   區分大小寫
-iname  不分區大小寫
?    可以表示任意一個單一的符號
*    可以表示任意數量(包括 0)的未知符號

#find /usr -name '*.txt'   #查找/usr目錄下所有文件名以.txt結尾的文件
#find /usr -name '????'    #查找/usr目錄下所有文件名剛好為4個字符的文件

#find /etc -name init
#find /etc -name *init*
#find /etc -name init???

#touch /tmp/inIt
#mkdir /tmp/Init
#find /tmp -name init
#find /tmp -iname init    #不區分大小寫
#find /tmp -iname ini*

有些時候,需要在搜索時匹配某個文件或目錄的完整路徑,而不僅僅是匹配文件名。可以使用 -path 或 -ipath 選項。
如查找/usr下所有文件名以.txt結尾的文件或目錄,且該文件的父目錄必須是src。可以使用以下命令:
#find /usr -path '*/src/*.txt'
-------------------------------------------------
2. 根據文件類型進行find檢索
如果只想搜索得到文件或目錄,即不想它們同時出現在結果中。可以使用 -type 選項指定文件類型。
-type 選項最常用的參數如下:
f:   文件
d:   目錄
l:   符號鏈接

# find /usr -type d -name 'python*'   #檢索/usr下所有文件名以python開頭的目錄
#find /etc -name init* -type f
#find /etc -name init* -a -type d

-a 兩個條件同時滿足
-o 兩個條件滿足一個即可
-------------------------------------------------
3. 根據文件大小進行find檢索
-size 選項允許用戶通過文件大小進行搜索(只適用於文件,目錄沒有大小)。

表示文件大小的單位由以下字符組成:
c:字節
k:Kb
M:Mb
G:Gb

另外,還可以使用 + 或 - 符號表示大於或小於當前條件。
#find / -size +204800       #查找大於100M的文件
#find / -size -204800       #查找小於100M的文件
#find / -size 204800        #查找等於100M的文件
#find / -size +1G           #檢索文件大小高於1GB的文件
#find / -size -10M          #檢索文件大小小於10M的文件

204800單位是數據塊
1數據塊=512字節=0.5K
100MB = 102400KB
100MB = 2048數據塊

#find /etc -size +163840 -a -size -204800   #查找大於80M小於100M的文件
# find / -size 50M
# find / -size +50M -size -100M
# find / -size +100M -exec rm -rf {} ;
# find / -type f -name *.mp3 -size +10M -exec rm {} ;
-------------------------------------------------
4. 檢索空文件
find 命令支持 -empty 選項用來檢索為空的文件或目錄。空文件即文件里沒有任何內容,空目錄即目錄中沒有任何文件或子目錄。
# find ~ -type d -empty    #檢索用戶主目錄下所有的空目錄
-------------------------------------------------
5. 反義匹配
find 命令也允許用戶對當前的匹配條件進行"反義"(類似於邏輯非操作)。
如需要檢索 /usr 下所有文件名不以 .txt 為后綴的文件。可以使用以下命令:
#find /usr -type f ! -name '*.txt'

也可以"翻轉"任何其他的篩選條件,如:
#find /usr -type f ! -empty 檢索 /usr 下所有內容不為空的文件
-------------------------------------------------
6. 根據文件的所屬權進行find檢索
#find / -type f -user starky  #檢索根目錄下所有屬主為starky的文件
#find / -user root 在根目錄下查找所有者為root的文件
#find / -group root 在根目錄下查找所屬組為root的文件

# find / -user root -name tecmint.txt
# find /home -user tecmint
# find /home -group developer
# find /home -user tecmint -iname "*.txt"
-------------------------------------------------
7. 根據時間日期進行find檢索

修改時間(Modification time):最后一次文件內容有過更改的時間點
訪問時間(Access time):最后一次文件有被讀取過的時間點
變更時間(Change time):最后一次文件有被變更過的時間點(如內容被修改,或權限等 metadata 被修改)
與此對應的是 find 命令中的 -mtime,-atime 和 -ctime 三個選項。

這三個選項的使用遵循以下示例中的規則:
-mtime 2: 該文件 2 天前被修改過
-mtime -2:該文件 2 天以內被修改過
-mtime +2:該文件距離上次修改已經超過 2 天時間

#find /usr -type f -mtime 2   #檢索/usr下兩天前被修改過的文件

如果覺得 -mtime 等選項以天為單位時間有點長,還可以使用 -mmin,-amin,-cmin 三個選項:
#find /usr -type f -mtime +50 -mtime -100    #檢索/usr下50到100天之前修改過的文件
#find /usr -type f -mtime 2 -amin 5          #檢索/usr下兩天前被修改過且5分鍾前又讀取過的文件

#find /etc -amin 5    #查找5分鍾之前被訪問過的文件和目錄
#find /etc -amin -5   #查找5分鍾之內被訪問過的文件和目錄
#find /etc -amin +5   #查找距離上次被訪問時間超過5分鍾過的文件和目錄

#find /etc -cmin 5    #查找5分鍾之前被修改過屬性的文件和目錄
#find /etc -cmin -5   #查找5分鍾之內被修改過屬性的文件和目錄
#find /etc -cmin +5   #查找距離上次被修改過屬性超過5分鍾的文件和目錄

#find /etc -mmin 5    #查找5分鍾之前被修改過內容的文件和目錄
#find /etc -mmin -5   #查找5分鍾之內被修改過內容的文件和目錄
#find /etc -mmin +5   #查找距離上次被修改過內容超過5分鍾的文件和目錄
-------------------------------------------------
8. 根據文件權限進行find檢索
find 命令可以使用 -perm 選項以文件權限為依據進行搜索。

使用符號形式
如需要檢索 /usr 目錄下權限為 rwxr-xr-x 的文件,可以使用以下命令:
# find /usr -perm u=rwx,g=rx,o=rx

搜索/usr目錄下所有權限為 r-xr-xr-x(即系統中的所有用戶都只有讀寫權限)的文件和目錄,可以使用以下命令:
#find /usr -perm a=rx

很多時候,只想匹配文件權限的一個子集。比如,檢索可以直接被任何用戶執行的文件,即只關心文件的執行權限,而不用管其讀寫權限是什么。
上述的需求可以通過以下命令實現:
#find / -type f -perm /a=x
其中 a=x 前面的 / 符號即用來表示只匹配權限的某個子集(執行權限),而不用關心其他權限的具體設置。

使用數字形式
-perm 選項也支持數字形式的文件權限標記。
# find /usr -perm 644     #搜索/usr目錄下權限為 644(即 rwxr-xr-x)的文件

# find . -type f -perm 0777 -print
# find / -type f ! -perm 777
# find / -perm 2644
# find / -perm 1551
# find / -perm /u=s
# find / -perm /g+s
# find / -perm /u=r
# find / -perm /a=x
# find / -type f -perm 0777 -print -exec chmod 644 {}\;
# find / -type d -perm 777 -print -exec chmod 755 {}\;
# find . -type f -name "tecmint.txt" -exec rm -f {} \;
# find . -type f -name "*.txt" -exec rm -f {} \;
# find . -type f -name "*.mp3" -exec rm -f {} \;
# find /tmp -type f -empty
# find /tmp -type d -empty
# find /tmp -type f -name ".*"
-------------------------------------------------
9. 限制遍歷的層數(這個在上面文章已經詳細介紹)
find 命令默認是以遞歸的方式檢索項目的,這有時候會導致得到的結果數量非常巨大。可以使用 -maxdepth 限制 find 命令遞歸的層數。
# find / -maxdepth 3 搜索時向下遞歸的層數最大為 3
-------------------------------------------------
10. 邏輯組合
在之前的例子中有出現多個搜索條件的組合以及對某個搜索條件的反轉。
實際上 find 命令支持 "and" 和 "or" 兩種邏輯運算,對應的命令選項分別是 -a 和 -o。通過這兩個選項可以對搜索條件進行更復雜的組合。

此外還可以使用小括號對搜索條件進行分組。注意 find 命令中的小括號常需要用單引號包裹起來。因小括號在 Shell 中有特殊的含義。

如檢索 /usr 下文件名以 python 開頭且類型為目錄的文件
#find /usr -type d -name 'python*'
該命令等同於:
#find /usr -type d -a -name 'python*'
更復雜的組合形式如:
#find / '(' -mmin -5 -o -mtime +50 ')' -a -type f
-------------------------------------------------
11. -exec command {} \;  以及 -ok command {} \; 用法
#find /etc -name inittab -exec ls -l {} \;     #查找inittab文件並顯示其詳細信息
在{}和\之間要有一個空格
-exec跟{}\; 之間執行的是對搜索結果進行的操作動作

#find /etc -name inittab -a -type f -exec ls -l {} \;
#touch /tmp/testfile.rm
#find /tmp -name testfile.* -exec rm {} \;

-ok等同於-exec
-ok跟{}\; 之間執行的是對搜索結果進行的操作動作
和-exec不同的地方在於有一個詢問,需要輸入y或n確認
#find /etc -name init* -ok rm -l {} \;
-------------------------------------------------
12. 根據inode節點進行find檢索
#find /etc -inum xxx     #根據I節點查找

#touch "test 000"
#ls -i
#find . -inum 396401 -exec rm {} \;

#touch test999
#ln test999 test9999
#ls -i test999
#find . -inum 396401 -exec ls -l {} \;
-------------------------------------------------
13. 對搜索結果執行命令
1)刪除文件
-delete 選項可以用來刪除搜索到的文件和目錄。

如刪除 home 目錄下所有的空目錄:
# find ~ -type d -empty -delete

2)執行自定義命令
-exec 選項可以對搜索到的結果執行特定的命令。

如需要將home目錄下所有的MP3音頻文件復制到移動存儲設備(假設路徑是/media/MyDrive),可使用下面的命令:
#find ~ -type f -name '*.mp3' -exec cp {} /media/MyDrive ';'

上面命令中的大括號{}作為檢索到的文件的占位符 ,而分號;作為命令結束的標志。因為分號是Shell中有特殊含義的符號,所以需要使用單引號括起來。
每當find命令檢索到一個符合條件的文件,會使用其完整路徑取代命令中的 {},然后執行 -exec 后面的命令一次。

另一個很重要的用法是,在多個文件中檢索某個指定的字符串。
如在用戶主目錄下的所有文件中檢索字符串 hello ,可以使用如下命令:
# find ~ -type f -exec grep -l hello {} ';'

-exec 選項中的 + 符號
現在假設需要將用戶主目錄下所有的MP3文件添加到壓縮包 music.tar.gz 中,直觀的感覺是,其命令應為如下形式:
#find ~ -type f -name '*.mp3' -exec tar -czvf music.tar.gz {} ';'

實際情況是,上面命令得到的music.tar.gz 其實只包含一個MP3文件。
原因是find命令每次發現一個音頻文件,都會再執行一次-exec選項后面的壓縮命令。導致先前生成的壓縮包被覆蓋。

可以先讓find命令檢索出所有符合條件的音頻文件,再將得到的文件列表傳遞給后面的壓縮命令。所以正確完整的命令如下:
#find ~ -type f -name '*.mp3' -exec tar -czvf music.tar.gz {} +

3)顯示文件信息
如果想瀏覽搜索到的文件(目錄)的詳細信息(如權限和大小等),可以直接使用-ls選項。
#find / -type file -size +1G -ls    #瀏覽所有 1G 以上大小的文件的詳細信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM