一,從索引庫查找檔案:locate
- 索引庫:作業系統會周期性的遍歷根檔案系統,然后生成索引庫
- 手動更新索引庫:
updatedb - 語法:
locate [OPTION]... PATTERN... - 只匹配basename:
-b - 統計出有多少個符合條件的檔案:
-c - 使用基本正則運算式:
-r - 注意:構筑索引,需要遍歷整個根檔案系統,非常消耗資源,
二,直接從檔案系統里查找:find
下面寫道的【檔案】,包含檔案和檔案夾
跟locate比,find是精確,實時查找,速度沒有locate快,
-
用法:
find [options] [查找起始路徑] [查找方式] [查找完,要進行去處理的動作] -
查找起始路徑:指定搜索目標的起始路徑,不指定則為當前目錄,
-
查找方式:指定查找的選項和方式,可以根據檔案名,檔案大小,檔案型別,從屬關系,mode等,
不指定查找方式則查找指定目錄下的所有檔案,
-
對查找出來的檔案,做出具體的操作,例如洗掉等,默認操作是把查找結果輸出到標準輸出,
**options : **
- 不查找子目錄:
-maxdepth 1
**查找方式: **
-
按檔案名字查找:
-name(區分檔案名的大小寫);-iname(不區分檔案名的大小寫)-name/-iname patternpattern:支持glob語法,但不支持正則運算式,
# ls test/ Passwd Passwd.txt # ls passwd passwd # find ./ -name passwd ./passwd # find ./ -iname passwd ./passwd ./test/Passwd Passwd.txt不是精確匹配,所以不符合查找條件, # find ./ -iname "passwd*" ./passwd ./test/Passwd ./test/Passwd.txt 由于使用了glob通配,Passwd.txt就符合查找條件了, # find ./ -iname "npasswd?" ./test/npasswdo ./test/npasswd- # find ./ -iname "npasswd[^[:alpha:]]" ./test/npasswd- # find ./ -iname "npasswd[[:alpha:]]" ./test/npasswdo -
按檔案的屬主查找:
-user name/uid-uid uid# find /tmp/ -user za1 /tmp/f1 # id za1 uid=1001(za1) gid=1001(za1) groups=1001(za1),1000(ys),1002(zg1),1004(gentoo) # find /tmp/ -user 1001 /tmp/f1 # find /tmp/ -uid 1001 /tmp/f1 -
按檔案的屬組查找:
-group name/gid-gid gid# find /tmp/ -group zg1 /tmp/f1 /tmp/d1/inittab # find /tmp/ -group 1002 /tmp/f1 /tmp/d1/inittab # find /tmp/ -gid 1002 /tmp/f1 /tmp/d1/inittab -
查找g屬組被洗掉了的檔案
# find /tmp/ -nogroup /tmp/f1 /tmp/d1/inittab /tmp/d1/issue /tmp/fld /tmp/test # ll -d f1 fld test -rw-r-----. 1 1001 1002 511 Dec 18 14:38 f1 drwxrwxr-x. 2 root 1002 6 Dec 17 22:39 fld drwxrwxr-x. 2 ys 1002 85 Dec 23 21:16 test # ll d1 -rwxr-xr--. 1 gentoo 1002 511 Dec 18 14:43 inittab -rwxr-xr--. 1 gentoo 1002 23 Dec 18 14:43 issue -
查找屬主被洗掉了的檔案
# find /tmp/ -nouser /tmp/f1 [root@localhost tmp]# ll f1 -rw-r-----. 1 1001 1002 511 Dec 18 14:38 f1 -
沒有屬主或屬組的檔案非常危險,所以要定期查看這些檔案,并把這些檔案的屬主和屬組分配出去,一般分給root,
-
根據檔案的型別查找:
-type TYPE-
f:普通檔案
-
d:目錄檔案
-
l:符號鏈接檔案
# find /etc/ -type l -
b:塊設備檔案
# find /dev -type b -ls 20033 0 brw-rw---- 1 root disk 253, 2 Dec 23 08:55 /dev/dm-2 12288 0 brw-rw---- 1 root disk 253, 1 Dec 23 08:55 /dev/dm-1 -
c:字符設備檔案
-
p:管道檔案
-
s:套接字檔案
-
-
組合查找:可以組合任意的查找方式,比如-name和-size等
- 與:-a 不指定默認就是-a,
- 或:-o
- 非:-not 或者!
練習1:找出/tmp目錄下屬主為非root,且檔案名不包含fstab的檔案,
注意:括號的用法,括號必須轉義,且括號兩側要有空格,
# find /tmp/ -not -user "root" -not -name "*fstab*" -ls | wc -l 98 # find /tmp/ -not -user "root" -a -not -name "*fstab*" -ls | wc -l 98 # find /tmp/ -not \( -user "root" -o -name "*fstab*" \) -ls | wc -l 98 -
根據檔案大小查找:
-size- 用法:`-size [+|-]#單位
- #代表數字(正數)
- 常用單位:K,M,G
- #UNIT:(#-1, #]
- -#UNIT:[0, #-1]
- +#UNIT:(#, 正無窮)
-
根據時間查找
-
以“天”為單位
-atime [+|-]#(#為數字(正數)):Access time
-
#:[#,#+1)
例如,#=1,那么就是查找[1,2)天前訪問過的檔案
# date Tue Dec 24 09:49:21 CST 2019 # stat /tmp/atime2 Access: 2019-12-22 08:35:00.000000000 +0800 # find /tmp/ -atime 1 #查找不到/tmp/atime2檔案,因為是大于2天沒訪問了 # touch -at 1912230800.00 /tmp/atime2 # stat /tmp/atime2 Access: 2019-12-23 08:00:00.000000000 +0800 # find /tmp/ -atime 1 /tmp/atime2 #大于1天并小于2天沒訪問了,所以找到了, # touch -at 1912231000.00 /tmp/atime2 Access: 2019-12-23 10:00:00.000000000 +0800 # find /tmp/ -atime 1 #查找不到/tmp/atime2檔案,因為小于1天沒有訪問, -
+#:[#+1,正無窮)
例如,#=1,那么就是查找[2,正無窮)天前訪問過的檔案
# date Tue Dec 24 10:03:57 CST 2019 # stat /tmp/atime2 Access: 2019-12-22 11:00:00.000000000 +0800 # find /tmp/ -atime +1 | grep "atime" #查找不到/tmp/atime2檔案,因為小于2天沒有訪問, # touch -at 1912221000.00 /tmp/atime2 # stat /tmp/atime2 Access: 2019-12-22 10:00:00.000000000 +0800 # find /tmp/ -atime +1 | grep "atime" /tmp/atime2#大于2天沒訪問了,所以找到了, -
-#:[0, #)
例如,#=1,那么就是查找[0,1)天前(24小時內)訪問過的檔案
# date Tue Dec 24 10:13:55 CST 2019 # stat /tmp/atime2 Access: 2019-12-23 10:12:00.000000000 +0800 find /tmp/ -atime -1 | grep "atime" #查找不到/tmp/atime2檔案,因為大于1天沒有訪問 # touch -at 1912231020.00 /tmp/atime2 # stat /tmp/atime2 Access: 2019-12-23 10:20:00.000000000 +0800 # find /tmp/ -atime -1 | grep "atime" /tmp/atime2#小于1天沒訪問了,所以找到了,
-mtime:Modify time
-ctime:Change time
-
-
以“分鐘”為單位
-amin [+|-]#(#為數字(正數)):Access time
-mmin:Modify time
-cmin:Change time
-
-
根據權限查找
-
語法:`-perm [-|/]mode
-
mode:精確匹配
# ll total 0 ---x--x--x. 1 root root 0 Dec 24 13:20 a -rw-r--r--. 1 root root 0 Dec 24 13:20 b -rw-r--r--. 1 root root 0 Dec 24 13:20 c -rw-r--r--. 1 root root 0 Dec 24 13:20 d -rw-r--r--. 1 root root 0 Dec 24 13:20 e # find ./ -perm 111 -ls 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a # find ./ ! -perm 111 -ls 1692200 0 drwxrwxr-x 2 ys 1002 69 Dec 24 13:20 ./ 1692205 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./b 1692207 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./c 1969792 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./d 1754172 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./e -
/mode:user || group || other
-
-mode:user && group && other
# ls -al total 12 drwxrwxr-x. 2 ys 1002 33 Dec 24 13:40 . drwxrwxrwt. 51 root root 8192 Dec 24 13:20 .. ---x--x--x. 1 root root 0 Dec 24 13:20 a -rwxrw-r--. 1 root root 0 Dec 24 13:20 b -rw-r--r--. 1 root root 0 Dec 24 13:20 c 屬主有執行權限或者屬組有寫權限的是查找物件 # find ./ -perm /120 -ls 1692200 0 drwxrwxr-x 2 ys 1002 33 Dec 24 13:40 ./ 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a 1692205 0 -rwxrw-r-- 1 root root 0 Dec 24 13:20 ./b 屬主沒有執行權限并且屬組沒有寫權限的是查找物件 # find ./ ! -perm /120 -ls 1692207 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./c 屬主有執行權限并且屬組有寫權限的是查找物件 # find ./ -perm -120 -ls 1692200 0 drwxrwxr-x 2 ys 1002 33 Dec 24 13:40 ./ 1692205 0 -rwxrw-r-- 1 root root 0 Dec 24 13:20 ./b 屬主沒有執行權限或者屬組沒有寫權限的是查找物件 # find ./ ! -perm -120 -ls 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a 1692207 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./c
-
-
-
查找完,要進行去處理的動作
-print:把查找結果輸出到標準輸出,默認的處理動作,
-ls:類似于對查找到檔案執行
ls -l命令,把結果輸出到標準輸出,-fls /PATH/NAME:類似于對查找到檔案執行
ls -l命令,把結果輸出指定檔案中,-delete:洗掉查找到的檔案,
-ok command {} ;:對查找到的每個檔案執行command命令,每次操作都需要用戶確認,{}就是find查找出來的檔案的檔案名
-exec command {} ;:對查找到的每個檔案執行command命令,不需要用戶確認,
注意:find是先查找完,然后把查找到的結果一次性傳遞給后面的命令;而不是查到一個傳遞一個,但是有些命令不能接受過長的引數,所以后面的命令就會執行失敗,使用另一種方式可以解決此問題,
find | xargs commandxargs會把find的結果進行分塊,就保證了后面的命令不會崩潰 ,
# find ./ ! -perm -120 ./a ./c [root@localhost test]# find ./ ! -perm -120 -ok cp {} {}.back \; < cp ... ./a > ? y < cp ... ./c > ? y [root@localhost test]# ll total 0 ---x--x--x. 1 root root 0 Dec 24 13:20 a ---x--x--x. 1 root root 0 Dec 24 14:40 a.back -rwxrw-r--. 1 root root 0 Dec 24 13:20 b -rw-r--r--. 1 root root 0 Dec 24 13:20 c -rw-r--r--. 1 root root 0 Dec 24 14:40 c.back # rm -f ./*.back [root@localhost test]# find ./ ! -perm -120 -exec cp {} {}.back \; [root@localhost test]# ll total 0 ---x--x--x. 1 root root 0 Dec 24 13:20 a ---x--x--x. 1 root root 0 Dec 24 14:41 a.back -rwxrw-r--. 1 root root 0 Dec 24 13:20 b -rw-r--r--. 1 root root 0 Dec 24 13:20 c -rw-r--r--. 1 root root 0 Dec 24 14:41 c.back
**練習: **
1,查找/var目錄下屬主為root,且屬組為mail的所有檔案或目錄,
# find /var -user root -group mail
/var/spool/mail
/var/spool/mail/root
# find /var -user root -group mail | xargs ls -ld
drwxrwxr-x. 2 root mail 167 Dec 23 16:42 /var/spool/mail
-rw-------. 1 root mail 463001 Dec 17 20:59 /var/spool/mail/root
2,查找/usr目錄下不屬于root,也不屬于bin,也不屬于gentoo的所有檔案或目錄,用兩種方法,
# find /usr ! -user root ! -user bin ! -user gentoo | wc -l
14
# find /usr ! \( -user root -o -user bin -o -user gentoo \) | wc -l
14
3,查找/tmp目錄下最近一周內其內容修改過,且屬主不是root用戶也不是gentoo用戶的檔案或目錄,
# find /tmp -mtime -7 ! -user root ! -user gentoo
4,查找當前系統上沒有屬主或屬組,且最近一周內被訪問過的檔案或目錄,
# find /tmp \( -nouser -o -nogroup \) -atime -7 -ls
67978820 4 -rw-r----- 1 1001 1002 511 Dec 18 14:38 /tmp/f1
67978822 4 -rwxr-xr-- 1 gentoo 1002 511 Dec 18 14:43 /tmp/d1/inittab
67978823 4 -rwxr-xr-- 1 gentoo 1002 23 Dec 18 14:43 /tmp/d1/issue
33599012 0 drwxrwxr-x 2 root 1002 6 Dec 17 22:39 /tmp/fld
1692200 0 drwxrwxr-x 2 ys 1002 33 Dec 24 14:43 /tmp/test
5,查找/tmp目錄下大于1M,且型別有普通檔案的所有檔案
# find /tmp -size +1M -type f | xargs ls -lh
-rw--w----. 1 root mageedu 2.3M Dec 18 10:44 /tmp/log/anaconda/journal.log
-rw--w----. 1 root mageedu 1.8M Dec 18 10:44 /tmp/log/audit/audit.log
-r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.1
-r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.2
-r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.3
-rw-rw-r--. 1 root mageedu 1.6M Dec 18 10:44 /tmp/log/firewalld
-rw-rw-r--. 1 root mageedu 1.2M Dec 18 10:44 /tmp/log/lastlog
-rw--w----. 1 root mageedu 1.1M Dec 18 10:44 /tmp/log/messages
-rw--w----. 1 root mageedu 4.4M Dec 18 10:44 /tmp/log/messages-20191206
-rw--w----. 1 root mageedu 49M Dec 18 10:44 /tmp/log/messages-20191215
6,查找/etc目錄下所有用戶都沒有寫權限的檔案
# find /etc ! -perm /222 -type f | xargs ls -l
-r--r--r--. 1 root root 460 Apr 11 2018 /etc/dbus-1/system.d/cups.conf
----------. 1 root root 920 Dec 23 21:38 /etc/gshadow
----------. 1 root root 927 Dec 23 21:33 /etc/gshadow-
-r--r--r--. 1 root root 63 Nov 9 2018 /etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
7,查找某個目錄目錄,至少有一類用戶沒有執行權限的檔案
# ll
-rwxr-xr-x. 1 root root 0 Dec 24 15:45 a
-rwxr--r--. 1 root root 0 Dec 24 15:45 b
-rw-r-xr--. 1 root root 0 Dec 24 15:45 c
-rw-r-xr-x. 1 root root 0 Dec 24 15:45 d
[root@localhost test1]# find ./ ! -perm -111 -type f | xargs ls -l
-rwxr--r--. 1 root root 0 Dec 24 15:45 ./b
-rw-r-xr--. 1 root root 0 Dec 24 15:45 ./c
-rw-r-xr-x. 1 root root 0 Dec 24 15:45 ./d
8,查找/etc/init.d/目錄下,所有用戶都有執行權限,且其他用戶有寫權限的所有檔案
# ll /etc/init.d/
total 40
-rw-r--r--. 1 root root 18281 Aug 24 2018 functions
-rwxr-xrwx. 1 root root 4569 Aug 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 Aug 24 2018 network
-rw-r--r--. 1 root root 1160 Oct 31 2018 README
# find /etc/init.d/ -perm -111 -perm /002 -type f -ls
101249165 8 -rwxr-xrwx 1 root root 4569 Aug 24 2018 /etc/init.d/netconsole
# find /etc/init.d/ -perm -113 -type f -ls
101249165 8 -rwxr-xrwx 1 root root 4569 Aug 24 2018 /etc/init.d/netconsole
# c/c++ 學習互助QQ群:877684253

# 本人微信:xiaoshitou5854
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/145052.html
標籤:Linux
上一篇:安全運維之檔案系統保護
下一篇:docker 資料持久化
