前文中,我們詳細闡述了linux中最復雜的Find命令的基礎查詢之八列屬性:

八列屬性
本文將繼續探討其高級查詢功能,將分為四個方面展開討論:
- 預定義動作 Predefined-Actions
- 自定義動作 User-defined Actions
- 與grep協同動作
- Operator邏輯操作
一、預定義動作 Predefined Actions
搜索是第一步,第二步是處理搜索的結果。比如刪除所有的搜索結果。
在Documents目錄下,搜索空文檔,然后刪除。
$ find ~/Documents -maxdepth 3 -empty -type f |nl
刪除操作需要第二步處理,我們使用循環結構和 read 命令
find ~/Documents -maxdepth 3 -empty -type f | while read line; do rm $line; done
為了避免每次都寫上 while read line; do rm $line; done 這一段,Find 命令提供了很多定義的行為。拿上面刪除搜索結果例子而言,只需后面加上 -delete 即可
find ~/Documents -maxdepth 3 -empty -type f -delete
我們前面已經接觸到了 -ls 這個預定義的行為
find ~/Documents -maxdepth 3 -empty -type f -ls
其他的預定義動作還有:
1) -print 打印當前的結果到標準輸出 (Terminal),這是默認的行為,不需要顯式標注
2) -print0 是文件名中的空格等換行符用空值表示,與xargs 的 -0 配合使用。
3) -quit 匹配到一個結果后退出查詢。
二、自定義動作 User-defined Actions
-print -ls 這些自定義動作,雖然很便捷,但其靈活性差的弊端也顯著。比如上例中的 -ls 只有一種顯示格式。
-exec (execute) 自定義執行
當需要更靈活多樣的顯示格式,需要引入 -exec (execute)
find ~/Documents -maxdepth 3 -empty -type f -exec ls -lh '{}' ';'
'{}' 指代前面所有的搜索結果,可以理解為前面的查詢結果都放入到了 {} 這個籃子里,后面的 ;為命令分割符。
ok代替exec
exec 的替代選項是 -ok,每次執行前都會彈出提示要用戶確認。處理刪除任務時,-ok 是更加安全的選項。
find ~/Documents -maxdepth 3 -empty -type f -ok rm '{}' ';'
+ 結束符
除了有以 ; 為命令的結束符,之外還有一個 + 結束符。
find ~/Documents -maxdepth 3 -empty -type f -ok ls -lh '{}' +
二者之間的區別是,當以 ; 結尾時,程序的實際執行過程為:
ls -lh file1
ls -lh file2
ls -lh file3
...
也就是對每個搜索結果逐個執行 ls 命令操作,這通常效率不高。而以 + 結尾則對上搜索就結果執行一次 ls 操作。
ls -lh file1 file2 file3 ...
同時還有與 xargs 相結合的方案,不推薦此方法,捎帶提一句。
find ~/Documents -maxdepth 3 -empty -type f | xargs -lh
# 等同于
find ~/Documents -maxdepth 3 -empty -type f -ok ls -lh '{}' +
三、與 grep 命令協同工作
搜索 Book.SICP 目錄下所有包含關鍵詞‘洞見’的文件,執行以下命令:
find . -type f -exec grep --color -nH --null -e '洞見' {} +
得到結果為:


四、邏輯操作
三種邏輯操作在 Find 命令中的選項分別為 -and(a), -or(o), -not
比如我們在上一講中,提到查詢各種類型的文件格式
find ~ -type f,d,l
使用’或‘邏輯將其改寫為:
find ~ ( -type f -or -type d -or -type l )
-not 的案例
find ~ ( -type f -not -perms 0600) -or ( -type d -not -perms 0700 )
-and 與邏輯是默認執行動作。
使用邏輯關系的基本表達是為:
expr1 -operator expr2
五、總結
我們以文件的七列屬性為藍本,逐次探討了Find的八個基本查詢功能,以及四個高階應用,總結如下:

find 總結
|----------+----------+----------------------------------------|
| 列號 | 名稱 | 方法 |
|----------+----------+----------------------------------------|
| 1 | 文件名 | -iname, -ipath, -regex |
|----------+----------+----------------------------------------|
| 2 | 時間戳 | -mtime(atime,ctime); -mmin(amin, cmin) |
|----------+----------+----------------------------------------|
| 3 | 文件大小 | -size(b,c,k,M,G) |
|----------+----------+----------------------------------------|
| 4 | 用戶組 | -group |
|----------+----------+----------------------------------------|
| 5 | 用戶 | -user |
|----------+----------+----------------------------------------|
| 6 | inode | -inum |
|----------+----------+----------------------------------------|
| 7 | 權限 | -type, -perm |
|----------+----------+----------------------------------------|
| 8 | 深度 | -mindepth,-maxdepth |
|----------+----------+----------------------------------------|
| Actions | 預定義 | -delete, -ls, -print, -print0 |
|----------+----------+----------------------------------------|
| Actions | 自定義 | -exec, -ok, xargs |
|----------+----------+----------------------------------------|
| Actions | 協同 | 與 grep 協同 |
|----------+----------+----------------------------------------|
| Operator | 邏輯操作 | -and, -or, -not |
|----------+----------+----------------------------------------
以上為 Find 查詢的全部內容。