0 引言
Linux find命令功能非常強大,往往在搜索定位時具有神奇的效果,可以簡化查詢操作流程,往往只需一條命令搞定。
1 命令基本用法
(1)語法格式
find [路徑] [選項] [操作]
(2)選型參數
選項 | 含義 |
-name | 根據文件名查找 |
-perm | 根據文件權限查找 |
-prune | 該選項可以排除某些查找目錄 |
-user | 根據文件屬主查找(所屬的用戶) |
-group | 根據文件屬組查找 |
-mtime -n | +n | 根據文件更改時間查找 |
-nogroup | 查找無有效屬組的文件 |
-nouser | 查找無有效屬主的文件 |
-newer file1 ! file2 | 查找更改時間比file1新但比file2舊的文件 |
-type | 按文件類型查找 |
-size -n +n | 按文件大小查找 |
-mindepth n | 查找子目錄的最小深度(按n級字目錄開始搜索) |
-maxdepth n | 查找子目錄的最大深度n(最多搜索到n級子目錄) |
2 案例實戰
(1)-name:查找/etc目錄下以conf結尾的文件
[root@bigdata3 ~]# find /etc -name '*conf'| head -n10
(2) -iname :查找當前目錄下文件名爲aa的文件(不區分大小寫)
[root@bigdata3 dan_test]# find . -iname aa
(3) -user 查找文件屬主爲hive的所有文件
[root@bigdata3 usr]# find . -user hive
(4) -group 查找文件屬組爲hadoop的所有文件
[root@bigdata3 usr]# find . -group hadoop | tail -n10
(4) -type
-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
(5)-size
-size
-n 小於大小n的文件
+n 大於小於n的文件
1) 查找/etc目錄下小於100000字節的文件
[root@bigdata3 etc]# find /etc -size -100000c | head -n10
2) 查找/etc目錄下大於1M的文件
[root@bigdata3 etc]# find /etc -size +1M
(6)-mtime
-mtime
-n n天以內修改的文件
+n n天以外修改的文件
n 正好n天修改的文件
1) 查找/etc目錄下50天之內修改且以conf結尾的文件
[root@bigdata3 etc]# find /etc -mtime -50 -name '*.conf'
2)查找/etc目錄下10天之前修改且屬主爲root的文件
[root@bigdata3 etc]# find /etc -mtime +50 -user root
-mmin
-n n分鐘以內修改的文件
+n n分鐘以外修改的文件
3)查找/etc目錄下30分鐘之前修改的文件
find /etc -mmin +30
4)查找/etc目錄下30分鐘之內修改的目錄
[root@bigdata3 etc]# find /etc -mmin -30 -type d
(7) -mindepth n
-mindepth n 表示從n級子目錄開始搜索
1) 在/etc下的6級子目錄開始搜索
[root@bigdata3 etc]# find /etc -mindepth 6
(8) -maxdepth n
-maxdepth n 表示最多搜索到n級子目錄
1) 在/etc下搜索符合條件的文件,但最多搜索到2級子目錄
find /etc -maxdepth 2 -name '*.conf'
2)搜索二級子目錄下的文件
[root@bigdata3 etc]# find /etc -mindepth 2 -maxdepth 2 -name '*.conf'
(9)-nouser
-nouser 查找沒有屬主的用戶
find . -type f -nouser
(10) -nogroup
-nogroup 查找沒有屬組的用戶
find . -type f -nogroup
(11) -perm
-perm 權限
查找文件爲777的文件
find . -perm 777
(12)-prune
通常和-path一起使用,用於將特定目錄排除在搜索條件之外
邏輯運算符:
-a 與
-o 或
-not或! 非
1)查找當前目錄下所有普通文件,但排除aa目錄
[root@bigdata3 dan_test]# find . -path ./aa -prune -o -type f
2)查找當前目錄下所有普通文件,但排除aa和AA目錄
find . -path ./aa -prune -o -path ./AA -prune -o -type f
注意:爲什麼排除兩個目錄的時候用-o(or)而不是-a(and)呢?我們可以這樣理解,若查找兩個目錄則用-a(A and B)相當於一個區 間內,排除的話相當於區間外,自然是or(-o).
3) 查找當前目錄下所有普通文件,但排除aa和AA目錄,但屬主爲hdfs
find . -path ./aa -prune -o -path ./AA -prune -o -type f -a -user hdfs
4)查找當前目錄下所有普通文件,但排除aa和AA目錄,但屬主爲hdfs,且文件大小必須大於500字節
find . -path ./aa -prune -o -path ./AA -prune -o -type f -a -user hdfs -a -size +500c
對於爲什麼-prune-o後面是-o而不是-a,這個解釋起來比較麻煩可理解爲prune的固定用法即可。
參考鏈接:
https://mp.csdn.net/console/editor/html/104557834
-prune用法很嚴格,若要忽略某個目錄一般採用如下固定模式:
find 查找文件的目錄 -path 需要排除的目錄 -prune -o -name 需要查詢的內容
注意事項:理解爲固定用法就可以了(-path->-prune->-o>-print)忽略四部曲
1)-prune 必須和 -path, -o 一起使用
2)-prune -o 的順序不 能調換
3)-name等必須放在-prune -o後面才能使用
4)如果後面有管道符號前面需要加-print參數
5)查找當前目錄下的屬主爲hdfs或者以xml結尾的普通文件
find . -type f -a \( -user hdfs -o -name '*.xml' \)
(13) -exec
-exec 對搜索到的文件執行特定的操作,固定格式爲-exec 'command' {} \;
-ok 和exec功能一樣,只是每次操作都會給用戶提示
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/ \;