常用參數:
-name 根據文件名查找
查找/etc目錄下以conf結尾的文件
find /etc -name '*conf'
-iname 根據文件名查找忽略大小寫
查找當前目錄文件名為abc的文件,不區分大小寫
find ./ -iname abc
-user 根據所屬用戶查找
查找當前目錄文件所有者為testuser的文件
find ./ -user testuser
-group 根據所屬用戶組查找
查找文件屬組為work的所有文件
find . -group work
-type 根據類型查找
- f 文件 find . -type f
- d 目錄 find . -type d
- c 字符設備文件 find . -type c
- b 塊設備文件 find . -type b
- l 鏈接文件 find . -type l
- p 管道文件 find . -type p
-size 根據大小查找
- -n 小于大小n的文件
- +n 大于小于n的文件
例1:查找/etc目錄下小于10000字節的文件
find /etc -size -10000c
例2:查找/etc目錄下大于1M的文件
find /etc -size +1M
-mtime 根據文件更改時間查找 (天)
- -n n天以內修改的文件
- +n n天以外修改的文件
- n 正好n天修改的文件
例1:查找/etc目錄下5天之內修改且以conf結尾的文件
find /etc -mtime -5 -name '*.conf'
例2:查找/etc目錄下10天之前修改且屬主為root的文件
find /etc -mtime +10 -user root
-mmin 根據文件更改時間查找 (分鐘)
- -n n分鐘以內修改的文件
- +n n分鐘以外修改的文件
例1:查找/etc目錄下30分鐘之前修改的文件
find /etc -mmin +30
例2:查找/etc目錄下30分鐘之內修改的目錄
find /etc -mmin -30 -type d
-mindepth n 表示從n級子目錄開始查找
例0:在/etc下的3級子目錄開始搜索
find /etc -mindepth 3
-maxdepth n 表示最多查找到n級子目錄
例1:在/etc下搜索符合條件的文件,但最多搜索到2級子目錄
find /etc -maxdepth 3 -name '*.conf'
例2:
find ./etc/ -type f -name '*.conf' -size +10k -maxdepth 2
了解參數:
-nouser 查找沒有屬主的用戶
find . -type f -nouser
-nogroup 查找沒有屬組的用戶
find . -type f -nogroup
-perm 根據用戶權限查找
find . -perm 664
-prune 排除特定目錄去查找
- 通常和-path一起使用,用于將特定目錄排除在搜索條件之外
例1:查找當前目錄下所有普通文件,但排除test目錄
find . -path ./etc -prune -o -type f
例2:查找當前目錄下所有普通文件,但排除etc和opt目錄
find . -path ./etc -prune -o -path ./opt -prune -o -type f
例3:查找當前目錄下所有普通文件,但排除etc和opt目錄,但屬主為hdfs
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs
例4:查找當前目錄下所有普通文件,但排除etc和opt目錄,但屬主為hdfs,且文件大小必須大于500字節
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs -a -size +500c
-newer file1
find /etc -newer a
操作:
-print 打印輸出
-exec 對搜索到的文件執行特定的操作,格式為-exec 'command' {} ;
例1:搜索/etc下的文件(非目錄),文件名以conf結尾,且大于10k,然后將其刪除
find ./etc/ -type f -name '*.conf' -size +10k -exec rm -f {} \;
例2:將/var/log/目錄下以log結尾的文件,且更改時間在7天以上的刪除
find /var/log/ -name '*.log' -mtime +7 -exec rm -rf {} \;
例3:搜索條件和例子1一樣,只是不刪除,而是將其復制到/root/conf目錄下
find ./etc/ -size +10k -type f -name '*.conf' -exec cp {} /root/conf/ \;
-ok 和exec功能一樣,只是每次操作都會給用戶提示
邏輯運算符:
-a 與
-o 或
-not | ! 非
例1:查找當前目錄下,屬主不是hdfs的所有文件
find . -not -user hdfs | find . ! -user hdfs
例2:查找當前目錄下,屬主屬于hdfs,且大小大于300字節的文件
find . -type f -a -user hdfs -a -size +300c
例3:查找當前目錄下的屬主為hdfs或者以xml結尾的普通文件
find . -type f -a \( -user hdfs -o -name '*.xml' \)