Linux 檔案查詢

luck-luck發表於2018-10-26

Linux檔案查詢
  檔案查詢:在檔案系統上需要根據檔案的各種屬性去查詢符合條件的檔案,而檔案查詢工具有兩個:locate和find
 此前使用的grep、egrep、fgrep屬於文字過濾、文字搜尋工具

  文檢查詢分為兩類:
  實時查詢:偏歷所有檔案進行條件匹配(find)
  非實時查詢:根據索引查詢(資料庫查詢)locate

locate
  工作特性:查詢速度快、模糊查詢、非實時查詢
  locate屬於非實時查詢,查詢系統上預建的檔案索引資料庫(/var/lib/mlocate/mlocate.db)
  依賴於事先構建的索引
  索引的建立是在系統空閒時由系統自動進行(週期性任務)
  管理員手動更新資料庫使用updatedb命令
  索引構建過程需要遍歷整個根檔案系統,極消耗資源
  搜尋的是檔案的全路徑,不僅僅是檔名
  可能只搜尋使用者具備讀取和執行許可權的目錄

  用法:
  locate [OPTION]… PATTERN…
  -b 只匹配路徑中的基名
  -c 統計出共有多少個符合條件的檔案
  -r 使用BRE(基本正規表示式)
  -i 不區分大小寫的搜尋

  注意:locate的用法非常簡單,即:locate KEYWORD,在locate後面跟上所要查詢的關鍵字即可,需依賴資料庫(缺點不能查詢指定目錄)

演示:
# 搜尋名稱或路徑中帶有“passwd”的檔案
[root@centos7 ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/htpasswd
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
/usr/lib/firewalld/services/kpasswd.xml
/usr/lib64/kde4/kded_kpasswdserver.so
/usr/lib64/samba/libsmbpasswdparser-samba4.so
/usr/lib64/samba/pdb/smbpasswd.so
/usr/lib64/security/pam_unix_passwd.so
/usr/sbin/chpasswd
/usr/sbin/lpasswd
/usr/sbin/saslpasswd2

# 加-b選項,只匹配路徑中的基名包含passwd
[root@centos7 ~]# locate -b passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/htpasswd
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
/usr/lib/firewalld/services/kpasswd.xml
/usr/lib64/kde4/kded_kpasswdserver.so
/usr/lib64/samba/libsmbpasswdparser-samba4.so

# 統計出共有多少個符合條件的檔案;
[root@centos7 ~]# locate -c passwd
140
[root@centos7 ~]# locate -c -b passwd
132

[root@centos7 ~]# locate -r `.foo$`
[root@centos7 ~]# locate -cr `.foo$`
0

find
  工作方式:實時查詢工具,通過遍歷指定起始路徑下系統層級結構完成檔案查詢

  工作特點:
  查詢速度略慢
  精確查詢
  實時查詢
  可能只搜尋使用者具備讀取和執行許可權的目錄

  用法:
  find [OPTION]… [查詢路徑] [查詢條件] [處理動作]
  查詢路徑:指定具體目標路徑,可指定多個路徑;預設為當前目錄
  查詢條件:指定的查詢標準,可以根據檔名、大小、型別、許可權等標準進行;預設為找出指定路徑下的所有檔案
  處理動作:對符合條件的檔案做操作,例如刪除等操作;預設操作為輸出至標準輸出(螢幕)

 查詢條件
  表示式:選項和測試
  測試:測試結果通常為布林型(真:true、假:false)

  根據檔名和inode查詢:
  -name “Partern”
  -iname “Partern” 不區分字母大小寫
    Partern支援使用glob風格的萬用字元(*, ?, [], [^]檔名稱一定加引號)
  -inum # 查詢inode號為#的
  -samefile FileName 與FileName檔案相同inode號的檔案
  -links # 查詢連結數為#的
  -regex “PATTERN” 以PATTERN匹配整個檔案路徑字串,而不僅僅是檔名稱

演示:
-name “partern”和-iname “partern”,支援使用glob風格第萬用字元
[root@centos7 ~]# mkdir /etc/test
[root@centos7 ~]# touch /etc/test/Passwd
[root@centos7 ~]# touch /etc/test/MPASSWD
[root@centos7 ~]# touch /etc/test/MPASSWD.txt

[root@centos7 ~]# find /etc -name passwd
/etc/passwd
/etc/pam.d/passwd

[root@centos7 ~]# find /etc -iname passwd
/etc/passwd
/etc/pam.d/passwd
/etc/test/Passwd

# 支援使用glob風格的通配
[root@centos7 ~]# find /etc/ -iname “passwd*”
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/test/Passwd

[root@centos7 ~]# find /etc/ -iname “*passwd”
/etc/passwd
/etc/pam.d/passwd
/etc/security/opasswd
/etc/test/Passwd
/etc/test/MPASSWD

[root@centos7 ~]# find /etc/ -iname “passwd?”
/etc/passwd-
[root@centos7 ~]# find /etc/ -iname “?passwd”
/etc/security/opasswd
/etc/test/MPASSWD

[root@centos7 ~]# touch /etc/test/passwdx
[root@centos7 ~]# find /etc/ -iname “passwd?”
/etc/passwd-
/etc/test/passwdx
[root@centos7 ~]# find /etc/ -iname “passwd[[:alnum:]]”
/etc/test/passwdx

  根據屬主、屬組查詢:
  -user UserName 查詢屬主為指定使用者的所有檔案
  -group GroupName 查詢屬組為指定組的所有檔案
  -uid UID 查詢屬主為指定的UID號的所有檔案
  -gid GID 查詢屬組為指定的GID號的所有檔案
  -nouser 查詢沒有屬主的檔案(檔案的屬主使用者被刪除)
  -nogroup 查詢沒有屬組的檔案

演示:
根據屬主,屬組查詢
[root@centos7 ~]# ll /home
總用量 0
drwx—— 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx—— 6 centos centos 134 2月 11 01:28 centos
drwx—— 3 mage mage 74 2月 14 19:35 mage
drwx——. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 根據屬主查詢
[root@centos7 ~]# find /home -user centos
/home/centos
/home/centos/.mozilla
/home/centos/.mozilla/extensions
/home/centos/.mozilla/plugins
/home/centos/.bash_logout
/home/centos/.bash_profile
/home/centos/.bashrc
/home/centos/.cache
/home/centos/.cache/abrt
/home/centos/.cache/abrt/lastnotification
/home/centos/.cache/dconf
/home/centos/.cache/dconf/user
/home/centos/.config
/home/centos/.config/abrt
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
# 根據屬組查詢
[root@centos7 ~]# find /home -group arclinux
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile
/home/arclinux/.bashrc
/home/arclinux/.cache
/home/arclinux/.cache/abrt
/home/arclinux/.cache/abrt/lastnotification
/home/arclinux/.config
/home/arclinux/.config/abrt
/home/arclinux/.bash_history

根據UID和GID查詢
[root@centos7 ~]# id centos
uid=1001(centos) gid=1001(centos) 組=1001(centos)
[root@centos7 ~]# id arclinux
uid=1002(arclinux) gid=1002(arclinux) 組=1002(arclinux),1003(mygrp)
[root@centos7 ~]# find /home -uid 1001
/home/centos
/home/centos/.mozilla
/home/centos/.mozilla/extensions
/home/centos/.mozilla/plugins
/home/centos/.bash_logout
/home/centos/.bash_profile
/home/centos/.bashrc
/home/centos/.cache
/home/centos/.cache/abrt
/home/centos/.cache/abrt/lastnotification
/home/centos/.cache/dconf
/home/centos/.cache/dconf/user
/home/centos/.config
/home/centos/.config/abrt
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
[root@centos7 ~]# find /home -gid 1002
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile
/home/arclinux/.bashrc
/home/arclinux/.cache
/home/arclinux/.cache/abrt
/home/arclinux/.cache/abrt/lastnotification
/home/arclinux/.config
/home/arclinux/.config/abrt
/home/arclinux/.bash_history

查詢沒有屬主和屬組的檔案
[root@centos7 ~]# ll /home
總用量 0
drwx—— 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx—— 6 centos centos 134 2月 11 01:28 centos
drwx—— 3 mage mage 74 2月 14 19:35 mage
drwx——. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 刪除一個使用者,保留其家目錄
[root@centos7 ~]# userdel mage
[root@centos7 ~]# ll /home
總用量 0
drwx—— 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx—— 6 centos centos 134 2月 11 01:28 centos
drwx—— 3 1003 1004 74 2月 14 19:35 mage # 刪除使用者後的目錄,沒有屬主和屬組
drwx——. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 查詢沒有屬主的檔案
[root@centos7 ~]# find /home -nouser
/home/mage
/home/mage/.mozilla
/home/mage/.mozilla/extensions
/home/mage/.mozilla/plugins
/home/mage/.bash_logout
/home/mage/.bash_profile
/home/mage/.bashrc

  根據檔案型別查詢
  -type TYPE
  f 普通檔案
  d 目錄檔案
  l 符號連結檔案
  s 套接字檔案
  b 塊裝置檔案
  c 字元裝置檔案
  p 管道檔案
  s 套接字檔案

演示:
# 查詢/dev下所有的塊裝置
[root@centos7 ~]# find /dev -type b
/dev/sr0
/dev/sda5
/dev/sda4
/dev/sda3
/dev/sda2
/dev/sda1
/dev/sda
[root@centos7 ~]# find /dev -type b -ls
9712 0 brw-rw—- 1 root cdrom 11, 0 2月 20 09:07 /dev/sr0
9705 0 brw-rw—- 1 root disk 8, 5 2月 20 09:07 /dev/sda5
9704 0 brw-rw—- 1 root disk 8, 4 2月 20 09:07 /dev/sda4
9703 0 brw-rw—- 1 root disk 8, 3 2月 20 09:07 /dev/sda3
9702 0 brw-rw—- 1 root disk 8, 2 2月 20 09:07 /dev/sda2
9701 0 brw-rw—- 1 root disk 8, 1 2月 20 09:07 /dev/sda1
9671 0 brw-rw—- 1 root disk 8, 0 2月 20 09:07 /dev/sda

  組合條件
  與 -a預設組合邏輯
  或 -o
  非 -not、!

  德·摩根定律:
  (非P) 或 (非Q) = 非(P且Q)
  (非P) 且 (非Q) = 非(P或Q)
  示例:
  !A -a !B = !(A -o B)
  !A -o !B = !(A -a B)

演示:
# 與邏輯
[root@centos7 ~]# find /home -nouser -type f
/home/mage/.bash_logout
/home/mage/.bash_profile
/home/mage/.bashrc
[root@centos7 ~]# find /home -nouser -type f -ls
67110217 4 -rw-r–r– 1 1003 1004 18 11月 20 2015 /home/mage/.bash_logout
67110218 4 -rw-r–r– 1 1003 1004 193 11月 20 2015 /home/mage/.bash_profile
67110219 4 -rw-r–r– 1 1003 1004 231 11月 20 2015 /home/mage/.bashrc

[root@centos7 ~]# find /home -nouser -a -type f -ls
67110217 4 -rw-r–r– 1 1003 1004 18 11月 20 2015 /home/mage/.bash_logout
67110218 4 -rw-r–r– 1 1003 1004 193 11月 20 2015 /home/mage/.bash_profile
67110219 4 -rw-r–r– 1 1003 1004 231 11月 20 2015 /home/mage/.bashrc

# 或邏輯
[root@centos7 ~]# find /home -nouser -o -type f -ls
67332181 4 -rw-r–r– 1 mageedu mageedu 18 11月 20 2015 /home/mageedu/.bash_logout
67332182 4 -rw-r–r– 1 mageedu mageedu 193 11月 20 2015 /home/mageedu/.bash_profile
67332183 4 -rw-r–r– 1 mageedu mageedu 231 11月 20 2015 /home/mageedu/.bashrc
67112341 4 -rw-r–r– 1 centos centos 18 11月 20 2015 /home/centos/.bash_logout
67112342 4 -rw-r–r– 1 centos centos 193 11月 20 2015 /home/centos/.bash_profile
67112343 4 -rw-r–r– 1 centos centos 231 11月 20 2015 /home/centos/.bashrc
134353167 4 -rw——- 1 centos centos 11 2月 13 11:29 /home/centos/.cache/abrt/lastnotification
134361164 4 -rw——- 1 centos centos 2 2月 11 01:32 /home/centos/.cache/dconf/user
67112299 4 -rw——- 1 centos centos 1257 2月 11 16:00 /home/centos/.bash_history
67114412 4 -rw——- 1 centos centos 105 2月 11 01:28 /home/centos/.local/share/keyrings/login.keyring
67114411 0 -rw——- 1 centos centos 0 2月 11 01:28 /home/centos/.local/share/keyrings/user.keystore
67110210 4 -rw-r–r– 1 arclinux arclinux 18 11月 20 2015 /home/arclinux/.bash_logout
67110211 4 -rw-r–r– 1 arclinux arclinux 193 11月 20 2015 /home/arclinux/.bash_profile
67110212 4 -rw-r–r– 1 arclinux arclinux 231 11月 20 2015 /home/arclinux/.bashrc
201391275 4 -rw——- 1 arclinux arclinux 11 2月 11 14:35 /home/arclinux/.cache/abrt/lastnotification
67110214 4 -rw——- 1 arclinux arclinux 217 2月 11 14:35 /home/arclinux/.bash_history

#非操作
[root@centos7 ~]# find /home -not -nouser
/home
/home/mageedu
/home/mageedu/.mozilla
/home/mageedu/.mozilla/extensions
/home/mageedu/.mozilla/plugins
/home/mageedu/.bash_logout
/home/mageedu/.bash_profile
/home/mageedu/.bashrc
/home/centos
/home/centos/.mozilla
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile

找出/tmp目錄下,屬主不是root,且檔名不以f開頭的檔案
# 組合條件查詢時,括號要轉義
find /tmp ( -not -user root -a -not -name `f*` ) -ls
# 或者
find /tmp -not ( -user root -o -name `f*` ) -ls

  根據檔案大小來查詢
  格式:-size [+|-] #UNIT(單位)
  常用單位:K、M、G
  區間:包含和不包含的關係(中括號包含等於,小括號不包含等於)
  #Unit  (#-1,#] 如:6k表示(5k,6k]
  -#Unit  [0,#-1] 如:-6k 表示[0,5k]
  +#Unit  (#,∞)  如:+6k 表示(6k,∞)

  根據時間戳查詢
 以”天”為單位,只能是過去的時間軸
 -atime [+|-]#,
  #   [#,#+1)  如:7 表示[7,8)從當前時刻起,過去第7天訪問的檔案(大於等於7小於第8天)
  +#  [#+1,∞] 如:+7 表示[8,∞]至少有7天沒有被訪問過了
  -#  [0,#)   如:-7 表示[0,7]7天內訪問過的檔案
  -mtime
  -ctime

  以”分鐘”為單位:
  -amin
  -mmin
  -cmin

演示:
# 查詢/etc目錄下至少已經一週沒有被訪問過的檔案,+7 為大於7天
[root@centos7 ~]# find /etc -atime +7 -ls
201367050 4 -rw-r–r– 1 root root 3562 11月 6 16:36 /etc/keepalived/keepalived.conf.bak
201367053 4 -rw-r–r– 1 root root 1555 11月 6 20:29 /etc/keepalived/keepalived.conf
134307865 4 -rw-r–r– 1 root root 577 8月 6 2015 /etc/odbcinst.ini
1049 4 -rw-r–r– 1 root root 1517 4月 20 2016 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
201366403 12 -rw-r–r– 1 root root 10341 11月 13 10:44 /etc/zabbix/zabbix_agentd.conf.bak
201366425 12 -rw-r–r– 1 root root 10575 11月 16 09:17 /etc/zabbix/zabbix_agentd.conf
201366446 16 -rw-r—– 1 root zabbix 15925 4月 20 2016 /etc/zabbix/zabbix_proxy.conf
134397465 20 -rw——- 1 root root 18861 11月 21 2015 /etc/snmp/snmpd.conf
134397466 4 -rw——- 1 root root 220 11月 21 2015 /etc/snmp/snmptrapd.conf
201643294 12 -rw-r–r– 1 root root 10017 12月 12 15:02 /etc/php-fpm.d/www.conf
134489903 4 -rw-r–r– 1 root root 2881 11月 21 2015 /etc/corosync/corosync.conf.example
134489904 4 -rw-r–r– 1 root root 767 11月 21 2015 /etc/corosync/corosync.conf.example.udpu
134489905 4 -rw-r–r– 1 root root 3278 11月 21 2015 /etc/corosync/corosync.xml.example
134489916 4 -rw-r–r– 1 root root 3031 12月 7 16:41 /etc/corosync/corosync.conf

# 查詢/etc目錄下一天以內修改的檔案
[root@centos7 ~]# find /etc -mtime -1 -ls
134299777 12 drwxr-xr-x 134 root root 8192 2月 20 10:45 /etc
134299808 4 -rw-r–r– 1 root root 80 2月 20 09:08 /etc/resolv.conf
134353171 4 -rw-r–r– 1 root root 1021 2月 20 10:36 /etc/group
134353172 4 ———- 1 root root 824 2月 20 10:36 /etc/gshadow
134361159 4 -rw-r–r– 1 root root 2467 2月 20 10:36 /etc/passwd
134353168 4 ———- 1 root root 1431 2月 20 10:36 /etc/shadow
67109024 4 drwxr-xr-x 6 root root 4096 2月 20 2017 /etc/sysconfig
35649 4 drwxr-xr-x 2 root root 4096 2月 20 2017 /etc/sysconfig/network-scripts
11249 4 -rw-r–r– 1 root root 14 2月 20 09:08 /etc/tuned/active_profile

[root@centos7 ~]# stat /etc/passwd
檔案:”/etc/passwd”
大小:2467 塊:8 IO 塊:4096 普通檔案
裝置:802h/2050d Inode:134361159 硬連結:1
許可權:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
最近訪問:2017-02-20 10:36:29.693358124 +0800
最近更改:2017-02-20 10:36:24.047176802 +0800
最近改動:2017-02-20 10:36:24.048176834 +0800
建立時間:-

 根據許可權查詢
  格式:-perm [/|-] MODE
  MODE 精確許可權匹配
  /MODE 任何一類(u,g,o)物件的許可權中的任何一位(r,w,x)符合條件既滿足,9位許可權之間為或關係
  -MODE 每一類使用者(u,g,o)的許可權中的每一位(r,w,x)同時符合條件既滿足,9位許可權之間為與關係
  查詢許可權位為”0″時:表示查詢匹配時該型別許可權不作為匹配條件

示例:
find -perm 755會匹配許可權模式恰好是755的檔案
find -perm /222只要當任意人有寫許可權時,就會匹配
find -perm -222只有當每個人都有寫許可權時,才會匹配
find -perm -002只有當其它人(other)有寫許可權時,才會匹配

演示:
[root@centos7 ~]# mkdir /tmp/test
[root@centos7 ~]# cd /tmp/test

[root@centos7 test]# touch a b c d e f g
[root@centos7 test]# ll
總用量 0
-rw-r–r– 1 root root 0 2月 20 13:41 a
-rw-r–r– 1 root root 0 2月 20 13:41 b
-rw-r–r– 1 root root 0 2月 20 13:41 c
-rw-r–r– 1 root root 0 2月 20 13:41 d
-rw-r–r– 1 root root 0 2月 20 13:41 e
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g
[root@centos7 test]# chmod 640 a
[root@centos7 test]# chmod 666 b
[root@centos7 test]# chmod 440 c
[root@centos7 test]# chmod 775 d
[root@centos7 test]# chmod 777 e

[root@centos7 test]# ll
總用量 0
-rw-r—– 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r–r—– 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g

# 精確匹配
[root@centos7 test]# find /tmp/test/ -perm 644 -ls
134361180 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/g

# 任何一類使用者的任何一位許可權符合條件即可
[root@centos7 test]# find /tmp/test/ -perm /666 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r—– 1 root root 0 2月 20 13:41 /tmp/test/a
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361166 0 -r–r—– 1 root root 0 2月 20 13:41 /tmp/test/c
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
134361180 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/g

# 查詢至少有一類使用者有寫許可權
[root@centos7 test]# find /tmp/test/ -perm /222 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r—– 1 root root 0 2月 20 13:41 /tmp/test/a
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
134361180 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/g

# 查詢其他使用者有寫許可權的檔案
[root@centos7 test]# find /tmp/test/ -perm /002 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e

#==================================================================================
# 查詢3類使用者同時擁有寫許可權的檔案
[root@centos7 test]# find /tmp/test/ -perm -222 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e

# 表示至少有一類使用者沒有寫許可權
[root@centos7 test]# find /tmp/test/ -not -perm -222 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r—– 1 root root 0 2月 20 13:41 /tmp/test/a
134361166 0 -r–r—– 1 root root 0 2月 20 13:41 /tmp/test/c
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361180 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r–r– 1 root root 0 2月 20 13:41 /tmp/test/g

[root@centos7 test]# find /tmp/test/ -perm -002 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e

# 表示屬組和其他只要有寫許可權即可
[root@centos7 test]# find /tmp/test/ -perm /022 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e

# 表示屬組和其他都必須有寫許可權才滿足
[root@centos7 test]# find /tmp/test/ -perm -022 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e

  處理動作
  -print  輸出至標準輸出,預設的動作
  -ls    類似於對查詢到的檔案執行”ls -l”命令,輸出檔案的詳細資訊
  -delete 刪除查詢到的檔案(不建議使用)
  -fls /PATH/TO/SOMEFILE 查詢到的所有檔案的長格式資訊儲存至指定檔案中(相當於重定向)
  -ok COMMAND {} ;  對查詢到的每個檔案執行由COMMAND指定的命令(對於每個檔案執行命令之前,都會互動式要求使用者確認,-exec不用確認,直接操作)
  -exec COMMAND {} ; 對查詢到的每個檔案執行由COMMAND指定的命令

  注意:
  find傳遞查詢到的檔案至後面指定的命令時,查詢到所有符合條件的檔案一次性傳遞給後面的命令
  有些命令不能接受過多引數(引數個數過多),此時命令執行可能會失敗,下面方式可規避此問題:find | xargs COMMAND
  xargs對查詢到的內容每次傳遞一個引數執行一次

演示:
[root@centos7 test]# chown arclinux.arclinux c d
[root@centos7 test]# ll
總用量 0
-rw-r—– 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r–r—– 1 arclinux arclinux 0 2月 20 13:41 c
-rwxrwxr-x 1 arclinux arclinux 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g

[root@centos7 test]# userdel -r arclinux
[root@centos7 test]# ll
總用量 0
-rw-r—– 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r–r—– 1 1002 1002 0 2月 20 13:41 c
-rwxrwxr-x 1 1002 1002 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g
[root@centos7 test]# find ./ -nouser
./c
./d
[root@centos7 test]# find ./ -nouser -a -nogroup -ls
134361166 0 -r–r—– 1 1002 1002 0 2月 20 13:41 ./c
134361178 0 -rwxrwxr-x 1 1002 1002 0 2月 20 13:41 ./d

[root@centos7 test]# find ./ -nouser -a -nogroup -ok chown root.root {} ; # 注意空格
< chown … ./c > ? y
< chown … ./d > ? y
[root@centos7 test]# ll
總用量 0
-rw-r—– 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r–r—– 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g

-exec COMMAND {} ;
[root@centos7 test]# find ./ -perm /002
./b
./e

# {}: 用於引用查詢到的檔名稱自身
[root@centos7 test]# find ./ -perm /002 -exec mv {} {}.danger ;
[root@centos7 test]# ll
總用量 0
-rw-r—– 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r–r—– 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r–r– 1 root root 0 2月 20 13:41 f
-rw-r–r– 1 root root 0 2月 20 13:41 g

練習:
# 備份配置檔案,新增.org這個副檔名,一定要注意格式,後面加斜槓和分號
find -name “*.conf” -exec cp {} {}.org ;
# 提示刪除存在時間超過3天以上的屬主為joe的臨時檔案
find /tmp -ctime +3 -user joe -ok rm {} ;
# 在你的主目錄中尋找可被其它使用者寫入的檔案,然後去掉寫許可權
find ~ -perm /002 -exec chmod o-w {} ;
# 找到/data 目錄下所有檔名以.sh字尾,且許可權為644的普通檔案,然後把許可權改為755
find /data –type f -perm 644 -name “*.sh” -exec chmod 755 {} ;

1、查詢/var目錄下屬主為root,且屬組為mail的所有檔案或目錄;
# find /var -user root -group mail
2、查詢/usr目錄下不屬於root、bin或hadoop的所有檔案或目錄;
# find /usr -not -user root -a -not -user bin -a -not -user hadoop
# find /usr -not ( -user root -o -user bin -o -user hadoop )
3、查詢/etc目錄下最近一週內其內容修改過,同時屬主不為root,也不是hadoop的檔案或目錄;
# find /etc -mtime -7 -a -not -user root -a -not -user hadoop
# find /etc/ -mtime -7 -a -not ( -user root -o -user hadoop )    括號中首尾有空格
4、查詢當前系統上沒有屬主或屬組,且最近一個周內曾被訪問過的檔案或目錄;
# find / -nouser -a -nogroup -a -atime -7
5、查詢/etc目錄下大於1M且型別為普通檔案的所有檔案;
# find /etc -size +1M -type f
6、查詢/etc目錄下所有使用者都沒有寫許可權的檔案;
# find /etc -not -perm /222
7、查詢/etc目錄下至少有一類使用者沒有執行許可權的檔案;
# find /etc -not -perm -111
8、查詢/etc/init.d目錄下,所有使用者都有執行許可權,且其它使用者有寫許可權的檔案;
# find /etc/init.d -perm -113

 

相關文章