概述
find指令常用于查找特定檔案,本期文章結合實際場景向各位小夥伴總結分享它的使用方法。
Tips:
測試作業系統:CentOS 8;
檔案屬性有哪些
Find指令可基于檔案屬性查找特定檔案,是以,知悉檔案屬性也就順理成章了。那麼檔案屬性有哪些呢?一張圖展示即可,如下圖所示;
場景1:基于檔案的類型辨別查找檔案
例如,查找/var/log/目錄下,普通檔案、連結檔案和目錄檔案;
Tips:預設遞歸查找到該目錄的子目錄
#查找普通檔案
[root@localhost ~]# find /var/log/ -type f
#查找連結檔案
[root@localhost ~]# find /var/log/ -type l
#查找目錄檔案
[root@localhost ~]# find /var/log/ -type d
場景2:基于檔案的權限配置設定查找檔案
首先,建立一些不同使用者權限的檔案,如下圖所示;
指令1:find -perm 220 -type f
指令1釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者和檔案所屬組都具有“寫”權限,其它使用者無權限,符合這些條件但設定了其他權限位的檔案(例如,如果有人可以執行該檔案)将不比對”的要求。
[root@localhost f_test]# find -perm 220 -type f
./f_220
[root@localhost f_test]#
指令2:find -perm -220 -type f
指令2釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者和檔案所屬組都具有“寫”權限,其它使用者無權限,不考慮是否存在任何額外的權限位(例如可執行位)。例如,這将比對具有模式0777的檔案”的要求。
[root@localhost f_test]# find -perm -220 -type f
./f_664
./f_665
./f_220
./f_223
./f_222
./f_777
[root@localhost f_test]#
指令3:find -perm -222 -type f
指令3釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者和檔案所屬組都具有“寫”權限,其它使用者具有“寫”權限,不考慮是否存在任何額外的權限位(例如可執行位)”的要求。
[root@localhost f_test]# find -perm -222 -type f
./f_223
./f_222
./f_777
[root@localhost f_test]#
指令4:find -perm /220 -type f
指令4釋義:目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者或檔案所屬組具有“寫”權限”的要求。
[root@localhost f_test]# find -perm /220 -type f
./f_664
./f_665
./f_202
./f_220
./f_223
./f_222
./f_777
[root@localhost f_test]#
指令5:find -perm /222 -type f
指令5釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者或檔案所屬組或其它使用者具有“寫”權限”的要求。
[root@localhost f_test]# find -perm /222 -type f
./f_664
./f_665
./f_102
./f_202
./f_220
./f_223
./f_222
./f_777
[root@localhost f_test]#
指令6:find -type f -perm -444 -perm /222 ! -perm /111
指令6釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的權限需符合“檔案所有者、檔案所屬組和其它使用者都具有“讀”權限并保證任意使用者具有“寫”權限而不具有“執行”權限”的要求。
[root@localhost f_test]# find -type f -perm -444 -perm /222 ! -perm /111
./f_664
[root@localhost f_test]#
場景3:基于檔案的大小查找檔案
首先,建立一些不同大小的檔案,如下圖所示;
指令1:find -type f -size 108M | xargs ls –lh
指令1釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的大小是“108MB”。
[root@localhost f_test]# find -type f -size 108M | xargs ls -lh
--w------- 1 root root 108M Jun 26 13:05 ./108/f_222
--w--w--w- 1 root root 108M Jun 26 11:41 ./f_222
[root@localhost f_test]#
指令2:find -type f -size +1G | xargs ls -lh
指令2釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的大小“大于1GB”。
[root@localhost f_test]# find -type f -size +1G | xargs ls -lh
-rwxrwxrwx 1 root root 1.6G Jun 26 11:39 ./f_777
[root@localhost f_test]#
指令3:find -type f -size +3c -size -20k | xargs ls -lh
指令3釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的大小“大于3位元組小于20KB”。
[root@localhost f_test]# find -type f -size +3c -size -20k | xargs ls -lh
---x----w- 1 root root 12K Jun 25 16:36 ./f_102
-rw-r--r-- 1 root root 4 Jun 26 11:29 ./f_111
--w-----w- 1 root root 11K Jun 26 08:56 ./f_202
[root@localhost f_test]#
指令4:find -type f -size +60M | xargs du -hm | sort -rn
指令4釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的大小“大于60MB”。根據查找到符合條件的檔案,再次查詢擷取檔案的大小并按照倒序的方式顯示。
[root@localhost f_test]# find -type f -size +60M | xargs du -hm | sort -rn
1627 ./f_777
139 ./f_665
108 ./f_222
108 ./108/f_222
[root@localhost f_test]#
場景4:基于檔案的名字查找檔案
指令1:find -type f -name "*222"
指令1釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案名尾包含資訊 “222”。
[root@localhost f_test]# find -type f -name "*222"
./f_222
./108/f_222
[root@localhost f_test]#
指令2:find -type f -name "?_222"
指令2釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案名頭比對一個字元并包含資訊 “_222”。
[root@localhost f_test]# find -type f -name "?_222"
./f_222
./108/f_222
[root@localhost f_test]#
指令3:find -type f -name "[0-9]*"
指令3釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案名頭包含資訊 “數字”。
[root@localhost f_test]# find -type f -name "[0-9]*"
./1111.txt
[root@localhost f_test]#
指令4:find -type f -name "[a-z]*"
指令4釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案名頭包含資訊 “字母”。
[root@localhost f_test]# find -type f -name "[a-z]*"
./f_664
./f_665
./f_102
./f_010
./f_202
./f_220
./f_223
./f_222
./f_777
./f_111
./108/f_222
./abc.c
[root@localhost f_test]#
指令5:find -type f -name \*
指令5釋義:在目前目錄及子目錄查找所有普通類型的檔案。
[root@localhost f_test]# find -type f -name \*
./f_664
./f_665
./f_102
./f_010
./f_202
./f_220
./f_223
./f_222
./f_777
[root@localhost f_test]#
場景5:基于檔案建立的時間查找檔案
目前時間點,如下所示;
[root@localhost log]# date
Mon Jun 26 15:20:32 CST 2023
[root@localhost log]#
指令1:find -type f -name "vmware-network*" -mtime +30 | xargs ls -l
指令1釋義:在/var/log/目錄及子目錄查找普通類型的檔案,并且,該檔案名頭包含資訊 “vmware-network”且該“檔案内容”30天前被修改。
[root@localhost log]# find -type f -name "vmware-network*" -mtime +30 | xargs ls -l
-rw------- 1 root root 719 May 25 13:07 ./vmware-network.2.log
-rw-------. 1 root root 719 May 25 10:41 ./vmware-network.3.log
-rw-------. 1 root root 741 May 24 14:54 ./vmware-network.4.log
-rw-------. 1 root root 719 May 24 14:02 ./vmware-network.5.log
[root@localhost log]#
指令2:find -type f -name "vmware-network*" -mtime -30 | xargs ls -l
指令2釋義:在/var/log/目錄及子目錄查找普通類型的檔案,并且,該檔案名頭包含資訊 “vmware-network”且該“檔案内容”30天内被修改。
[root@localhost log]# find -type f -name "vmware-network*" -mtime -30 | xargs ls -l
-rw------- 1 root root 741 Jun 21 16:12 ./vmware-network.1.log
-rw------- 1 root root 719 Jun 25 09:42 ./vmware-network.log
[root@localhost log]#
場景6:基于檔案所屬者或所屬組查找檔案
指令1:find -type f -user test | xargs ls -l
指令1釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的所屬者是test。
[root@localhost f_test]# find -type f -user test | xargs ls -l
-rw-r--r-- 1 test root 4 Jun 26 11:29 ./f_111
-rwxrwxrwx 1 test Test 1705703150 Jun 26 11:39 ./f_777
[root@localhost f_test]#
指令2:find -type f -group Test | xargs ls -l
指令2釋義:在目前目錄及子目錄查找普通類型的檔案,并且,該檔案的所屬組是Test。
[root@localhost f_test]# find -type f -group Test | xargs ls -l
--w--w--w- 1 root Test 112539936 Jun 26 11:41 ./f_222
-rwxrwxrwx 1 test Test 1705703150 Jun 26 11:39 ./f_777
[root@localhost f_test]#
總結
結合具體的使用場景詳細說明了find指令的使用方法,其它用法可通過指令man find檢視。以上分享,希望各位小夥伴有所收獲,歡迎各位點贊收藏和指正。