寫在前面:
博客書寫牢記5W1H法則:What,Why,When,Where,Who,How。
本篇主要內容:
● locate用法
● 使用find基於文件屬性進行查找
locate與find簡介:
locate與find都是文件查找工具(區別與grep等文本查找工具),尤其是find,可根據文件的諸多屬性(如文件大小、屬主屬組、權限信息、修改時間等),對文件進行實時查找
locate:
1、查找默認數據庫文件/var/lib/mlocate/mlocate.db,所以查找速度快,非實時,每天自動更新數據庫。手動更新用updatedb命令。
2、既然直接查找數據庫文件,所以也不用指定查找路徑。
3、自動更新遵循配置文件/etc/updatedb.conf。會排除一些目錄及某些字符串結尾的文件。
4、默認情況下,PATTERN包含在整個路徑中,不是精確匹配文件名。
find:
實時查找工具,通過遍歷指定路徑下的文件系統完成文件查找;
工作特點:
查找速度略慢;
精確查找;
實時查找;
locate
find files by name
locate [OPTION]... PATTERN...
-b, --basename:只匹配基名
-i, --ignore-case
-r, --regexp REGEXP
默認支持通配符(glob)
實例:
#查找文件或路徑名包含file的文件 [root@localhost ~]# locate 'file' /usr/share/vim/vimfiles/after/lang /var/db/Makefile /var/lib/yum/rpmdb-indexes/file-requires 省略輸出... #使用locate查找以".repo"結尾的文件或目錄 locate -b -r '\.repo$'
find
search for files in a directory hierarchy
find [-L] [-P] [path...] [expression]
GNU find會自左向右的查找以給定目錄爲根的目錄樹,知道查找到結果後,會繼續匹配下一個文件。find默認支持使用glob(通配符),欲使用正則表達式,需要使用額外選項。
-P:不展開軟鏈接,當搜索到軟鏈接時,按鏈接本身對待,而不是查找原文件。默認選項。
-L:展開軟鏈接,當搜索到指向目錄的軟鏈接時,查看的文件類型是目錄內文件的,而不是軟鏈接本身。除非鏈接損壞。
語法:
find [OPTION]... [查找路徑] [查找條件] [處理動作]
OPTION:
-maxdepth levels:查找的最深路徑,當前目錄下是1
-mindepth levels:查找的最淺路徑
查找條件:
根據文件名稱:
-name pattern:匹配文件名,只匹配基名
-iname pattern:文件名忽略大小寫,只匹配基名
-regex pattern:匹配整個路徑,而不只是基名
實例:
#查找/etc/目錄,文件名爲*.repo的文件 find /etc -name '*.repo' #查找/etc/目錄,文件名中包含忽略大小寫"centos"的文件 find /etc -iname '*centos*' #查找整個路徑中以shell/1.sh結尾的文件,例如想查找文件名爲1.sh的文件,但只記得其父目錄爲shell,不記得其他信息: find / -regex '.*shell/1.sh'
根據屬主、屬組查找:
-user uname
-uid n
-group gname
-gid n
-nouser
-nogroup
實例:
#查找/var /home /etc /tmp目錄中沒有屬主的文件 find /var /home /etc /tmp -nouser
根據文件類型查找:
-type c
文件類型字符表示:b(block) c(charecter) d(directory) p(pipe) f(regular file) l(symbolic link) s(socket) D(door)
實例:
#查找/dev目錄下,文件類型爲塊設備(block),名稱以sd開頭的文件 find /dev -type b -name 'sd*'
條件組合:
\( expr \):分組,注意兩邊都保留空格
! expr或-not expr:邏輯非
expr1 expr2或expr1 -a expr2或expr1 -and expr2:邏輯與
expr1 -o expr2或expr1 -or expr2:邏輯或
expr1 , expr2:只匹配expr2的結果,expr1不起作用
條件組合法則:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
實例:
#找出/tmp目錄下,屬主不是root,且文件名不是fstab的文件 find /tmp -not -user root -a -not -name 'fstab' 或 find /tmp -not \( -user root -o -name 'fstab' \) #找出/tmp目錄下,屬主不是root,或屬組不是root的文件 find /tmp \( -not -user 'root' \) -o \( -not -group 'root' \) 或 find /tmp -not \( -user 'root' -a -group 'root' \)
根據文件大小來查找:
-size [+|-]n[cwbkMG]
單位:c(bytes) w(two-byte words) b(512-byte blocks,默認) k(1024bytes) M(1048576 bytes) G(1073741824 bytes)
+n:表示大於n單位
-n:表示小於n單位
find的計算方式與ls -l不同,如查找2k的文件,find會找到1-2k的文件
#查找/etc目錄下,文件大小爲1M的文件 [root@localhost ~]# find /etc -size 2M /etc/selinux/targeted/contexts/files/file_contexts.bin [root@localhost ~]# ls -lh /etc/selinux/targeted/contexts/files/file_contexts.bin -rw-------. 1 root root 1.3M Mar 8 22:58 /etc/selinux/targeted/contexts/files/file_contexts.bin #可以看到文件大小隻有1.3M,但卻被find理解爲2M的文件,請一定注意! #查找/etc目錄下,文件大小大於2M的文件 [root@localhost ~]# find /etc -size +2M | xargs ls -ldh -rw-r--r--. 1 root root 3.6M Mar 8 22:58 /etc/selinux/targeted/policy/policy.29 -r--r--r--. 1 root root 6.7M Mar 3 18:59 /etc/udev/hwdb.bin #查找/etc目錄下,文件大小小於2M的文件 [root@localhost ~]# find /etc -size -2M | grep file_contexts.bin [root@localhost ~]# #可以看到查找結果中並沒有上面例子中出現的文件大小爲1.3M的文件,因爲find會認爲小於2M是0-(2-1)M。
根據時間戳:
-atime [+|-]n:atime屬性在第n天[前|內]
-ctime [+|-]n:ctime屬性。。。
-mtime [+|-]n:mtime。。。
-amin [+|-]n:atime在第n分鐘[前|內]
-cmin [+|-]n:ctime。。。
-mmin [+|-]n:mtime。。。
-anewer file:atime比file文件新(晚)
-cnewer file:ctime。。。
-newer file:mtime。。。
注意:以天爲單位,從0開始計數,0表示過去24小時,如圖:
實例:
“2”表示48-72小時之間,即24n到24(n+1)小時之間 [root@localhost ~]# date +"%F %T" 2016-03-18 08:35:39 [root@localhost ~]# find /etc -mtime 2 | xargs ls -ld --full-time -t ---------- 1 root root 689 2016-03-15 23:35:52.867508605 +0800 /etc/gshadow -rw-r--r-- 1 root root 841 2016-03-15 23:35:52.864508553 +0800 /etc/group -rw-------. 1 root root 834 2016-03-15 23:35:51.000000000 +0800 /etc/group- -rw-------. 1 root root 682 2016-03-15 23:35:51.000000000 +0800 /etc/gshadow- -rw-r--r-- 1 root root 1654 2016-03-15 23:32:58.909255137 +0800 /etc/passwd -rw-r--r--. 1 root root 1613 2016-03-15 22:46:40.000000000 +0800 /etc/passwd- ----------. 1 root root 927 2016-03-15 22:46:40.000000000 +0800 /etc/shadow- -rw-r--r-- 1 root root 921 2016-03-15 19:13:48.782124808 +0800 /etc/fstab -rw-r--r-- 1 root root 1237 2016-03-15 13:49:13.463941182 +0800 /etc/blkid/blkid.tab.old “-2”表示0-48小時之間,即0到24n小時 [root@localhost ~]# find /etc -mtime -2 | xargs ls -ld --full-time -t -rw-r--r--. 1 root root 303 2016-03-18 08:27:07.635063121 +0800 /etc/printcap drwxr-xr-x. 5 root lp 4096 2016-03-18 08:27:07.565063121 +0800 /etc/cups -rw-r----- 1 root lp 128 2016-03-18 08:27:07.485063126 +0800 /etc/cups/classes.conf -rw------- 1 root lp 659 2016-03-18 08:27:07.337062985 +0800 /etc/cups/printers.conf drwxr-xr-x. 103 root root 12288 2016-03-18 08:26:34.260063131 +0800 /etc -rw-r--r-- 1 root root 228 2016-03-18 08:26:34.260063131 +0800 /etc/resolv.conf drwx------. 2 root root 4096 2016-03-17 18:06:49.561287004 +0800 /etc/lvm/cache -rw------- 1 root root 2576 2016-03-17 18:06:49.561287004 +0800 /etc/lvm/cache/.cache ---------- 1 root root 1052 2016-03-17 09:35:42.271915599 +0800 /etc/shadow -rw-r----- 1 root lp 128 2016-03-17 09:29:43.361000123 +0800 /etc/cups/classes.conf.O -rw------- 1 root lp 659 2016-03-17 09:29:43.326001306 +0800 /etc/cups/printers.conf.O -rw-r--r-- 1 root root 1314 2016-03-17 09:29:17.357999989 +0800 /etc/tpvmlp.conf -rw-r--r-- 1 root root 415 2016-03-17 09:28:58.034999999 +0800 /etc/mtab -rw-r--r--. 1 root root 46 2016-03-16 17:37:01.498334131 +0800 /etc/adjtime drwxr-xr-x 5 root root 4096 2016-03-16 17:36:57.758334132 +0800 /etc/vmware-tools drwx------. 2 root root 4096 2016-03-16 12:30:04.227061013 +0800 /etc/lvm/backup -rw------- 1 root root 1993 2016-03-16 12:30:04.227061013 +0800 /etc/lvm/backup/myvg drwx------. 2 root root 4096 2016-03-16 12:30:04.189060531 +0800 /etc/lvm/archive -rw------- 1 root root 1383 2016-03-16 12:30:04.189060531 +0800 /etc/lvm/archive/myvg_00001-1703594395.vg -rw------- 1 root root 1399 2016-03-16 12:28:02.558640976 +0800 /etc/lvm/archive/myvg_00000-342243469.vg drwxr-xr-x. 2 root root 4096 2016-03-16 12:21:03.908021043 +0800 /etc/blkid -rw-r--r-- 1 root root 1237 2016-03-16 12:21:03.908021043 +0800 /etc/blkid/blkid.tab -rw------- 1 root root 1348 2016-03-16 12:03:58.229860544 +0800 /etc/lvm/archive/vg1_00001-2005147032.vg -rw------- 1 root root 1378 2016-03-16 12:03:07.358648129 +0800 /etc/lvm/archive/vg1_00000-650732143.vg “+2”代表72小時以前,即大於24(n+1) [root@localhost ~]# find /etc -mtime +2 | xargs ls -ld --full-time -t | grep gshadow [root@localhost ~]#
#查找/tmp目錄下,訪問時間在24小時內的文件並詳細顯示 [root@localhost ~]# date +"%F %T" 2016-03-10 10:37:26 [root@localhost ~]# find /tmp -atime 0 | xargs ls -ld --time atime -t --full-time drwxrwxrwt. 16 root root 4096 2016-03-10 10:35:34.037896700 +0800 /tmp drwxr-x-w-. 2 root root 18 2016-03-09 21:54:19.443113494 +0800 /tmp/xx -rw-r--r--. 1 root root 58 2016-03-09 18:30:40.551043611 +0800 /tmp/date.log -rw-r--r--. 1 root root 196 2016-03-09 18:30:40.551043611 +0800 /tmp/memory.txt drwxr-xr-x. 2 root root 38 2016-03-09 11:13:18.426879619 +0800 /tmp/vmware-config0 drwx------. 2 root root 4096 2016-03-09 11:13:18.426879619 +0800 /tmp/vmware-root drwxrwxr-x. 3 mageedu mageedu 18 2016-03-09 11:13:17.844872897 +0800 /tmp/mageedu #查找當前路徑下,比functions.bak文件內容改動時間更新(晚)的文件 [root@localhost ~]# ls -l --full-time functions.bak -rw-r--r--. 1 root root 14391 2016-03-09 16:46:30.772901402 +0800 functions.bak [root@localhost ~]# find . -newer functions.bak | xargs ls -ld --full-time -t drwxr-xr-x. 2 root root 29 2016-03-10 10:32:38.310467090 +0800 ./.cache/abrt -rw-------. 1 root root 11 2016-03-10 10:32:38.309467082 +0800 ./.cache/abrt/lastnotification dr-xr-x---. 7 root root 4096 2016-03-10 10:32:38.297466990 +0800 . -rw-------. 1 root root 268 2016-03-10 10:32:38.297466990 +0800 ./.Xauthority -rw-------. 1 root root 20626 2016-03-10 10:28:12.176531057 +0800 ./.bash_history -rw-------. 1 root root 6877 2016-03-10 10:28:05.687488818 +0800 ./.viminfo -rw-------. 1 root root 81 2016-03-09 23:47:30.380374947 +0800 ./.lesshst drwxr-xr-x. 2 root root 54 2016-03-09 22:12:39.964026062 +0800 ./test -rw-r--r--. 1 root root 0 2016-03-09 22:12:39.964026062 +0800 ./test/1.sh -rw-r--r--. 1 root root 527 2016-03-09 17:27:20.207755863 +0800 ./.ssh/known_hosts
根據權限查找:
-perm mode:指定準確權限
-perm -mode:ugo三組都必須滿足mode,實際權限可以大於mode
-perm /mode:ugo三組有一組滿足mode即可
實例:
#查找$PATH路徑下下,所有用戶都有執行權限,且其它用戶有寫權限的普通文件 [root@localhost ~]# find `echo $PATH | tr ':' ' '` -perm -113 -type f 省略輸出.... /usr/bin/screen /usr/bin/koi8rxterm /usr/bin/resize /usr/bin/stap-merge /usr/bin/stap-report /usr/bin/catman #查找/etc目錄下至少有一類用戶沒有執行權限的文件; [root@localhost ~]# find /etc/ -not -perm -111 省略輸出.... -rw-r--r--. 1 root root 6 Dec 9 17:59 /etc/yum/vars/infra -rw-r--r--. 1 root root 444 Dec 3 23:33 /etc/yum/version-groups.conf -rw-r--r--. 1 root root 2534 Dec 3 23:33 /etc/yum/yum-cron.conf -rw-r--r--. 1 root root 2496 Dec 3 23:33 /etc/yum/yum-cron-hourly.conf #查找/etc目錄下所有用戶都沒有寫權限的文件 [root@localhost ~]# find /etc/ -not -perm /222 | xargs ls -ld -r--r--r--. 1 root root 460 Nov 20 23:07 /etc/dbus-1/system.d/cups.conf ----------. 1 root root 742 Mar 9 11:03 /etc/gshadow ----------. 1 root root 731 Mar 8 10:45 /etc/gshadow- 省略輸出....
處理動作:
-delete:直接刪除查找到的內容,不會提示!!!
-exec command {} \; :其中{}代表前面查到的內容
-fls file:執行-ls動作,並且把結果保存至文件
-ls:相當於ls -dils,只顯示目錄,顯示inode,長格式,以blocks顯示文件大小
-ok command {} \; 與exec類似,但會每一步操作都提示用戶
-print:輸出到屏幕,默認動作
-prune:修剪。排除
實例:
#查找當前目錄,並排除dir1目錄,名稱以file開頭的文件 [root@localhost test]# mkdir dir1 dir2 [root@localhost test]# touch dir1/file{1..3} [root@localhost test]# touch dir2/file{1..3} [root@localhost test]# find . -path ./dir1 -prune -o -name file* -print ./dir2/file1 ./dir2/file2 ./dir2/file3 #查找/var/log目錄,大於1M的文件,並提示是否刪除 [root@localhost ~]# find /var/log -size +1M -ok rm {} \; < rm ... /var/log/audit/audit.log > ? n < rm ... /var/log/messages-20160308 > ? n #查找/etc目錄下最近一週內其內容修改過,同時屬主不爲root,也不是hadoop的文件或目錄,將結果保存至/tmp/weekmodify.result。 [root@localhost man1]# find /etc -mtime -7 -not -user 'root' -not -user 'hadoop' -fls /tmp/weekmodify.result [root@localhost man1]# cat /tmp/weekmodify.result 201327388 0 drwx------ 2 polkitd root 63 Mar 4 02:44 /etc/polkit-1/rules.d