find命令詳解

今天,來分享一下find命令的使用方法。
find: 與其他的查找命令相比,如locate,find的查找是實時的,精確的查找命令,但其缺點是查找速度慢。
find使用方法基本可以總結爲:

find 查找路徑 查找標準 處理動作

查找路徑:你想要查找的文件路徑,默認爲當前路徑
查找標準:查找的條件,默認爲查找路徑下的所有文件
處理動作:查找到文件後要做的動作,默認爲顯示

查找標準:

  • -name FILE :根據文件名FILE查找
    • *:代表任意長度字符
    • ?:任意單個字符
    • [ ]:括號裏面的字符中的任意一個
  • iname:不區分大小寫
  • regex PATTERN:基於正則表達式PATTERN進行查找
  • user:根據文件屬主查找
  • group:根據文件屬組查找
  • uid:根據文件屬主的uid查找
  • gid:根據文件屬組的gid查找
  • nouser:查找沒有屬主的文件
  • nogroup:查找沒有屬組的文件
  • type:根據文件類型查找
    • f:普通文件
    • d:目錄
    • c:字符文件
    • l:鏈接文件
    • s:套接字文件
    • p:管道文件
    • b:塊設備文件
  • -size:根據文件大小查找
    • [+|-] # k:[大於|小於]#KB的文件
    • [+|-] # M:[大於|小於]#KB的文件
    • [+|-] # G:[大於|小於]#KB的文件
  • -mtine [+|-] #:#天[外|內]內容被修改的文件
  • -ctime [+|-] #:#天[外|內]被更改的文件
  • -atime [+|-] # :#天[外|內]被訪問的文件
  • -mmin [+|-] #:#分鐘[外|內]內容被修改的文件
  • -cmin [+|-] #:#分鐘[外|內]被更改的文件
  • -amin [+|-] # :#分鐘[外|內]被訪問的文件
    注:關於ctime與mtime的區別,戳這裏:ctime與mtime區別
  • perm -MODE:按MODE權限查找,每一位都必須包含
  • perm /MODE :按MODE權限查找,任何一位包含都可以
  • perm MODE :按MODE權限查找,必須文件匹配,每一位都必須與MODE相同

處理動作:

  • -print:顯示
  • -ls :顯示詳細信息
  • -ok COMMAND {} \; :操作需用戶確認
  • -exec COMMAND {} \;:操作而不需用戶確認

組合條件:

  • -a : 與
  • -o:或
  • -not:非

注:默認爲與操作

舉例:

1.找出/etc/下,所有名爲passwd的文件和以ifcfg開頭的文件:

[root@DesktopComputer /tmp/test 13:31:31 ]$ find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@DesktopComputer /tmp/test 13:31:50 ]$find /etc/ -name ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-Profile_1
/etc/sysconfig/network-scripts/ifcfg-enp3s0
[root@DesktopComputer /tmp/test 13:34:48 ]$ 

2.找出/bin/下,以z結尾的文件

[root@DesktopComputer /tmp/test 13:50:08 ]$ find /bin/ -regex '.*z$' 
/bin/unxz
/bin/xz
/bin/lz
/bin/dwz
/bin/tgz
/bin/uz
/bin/rz
/bin/sz
[root@DesktopComputer /tmp/test 13:50:45 ]$ 

3.找出系統中,沒有屬主或屬組的文件

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

注:等待時間較長,如果發現找到,請刪掉這些無用文件

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup -exec rm {} \;
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

4.查找/etc/下有讀寫權限的鏈接文件

[root@DesktopComputer /tmp/test 14:17:14 ]$ find /etc/ -type l -a -perm -666
/etc/mtab
/etc/fonts/conf.d/65-0-lohit-telugu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-wqy-microhei.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/65-0-lohit-marathi.conf
...

注:所有的鏈接文件都爲777,可以通過一下方式驗證:

[root@DesktopComputer /tmp/test 14:17:24 ]$ find /etc/ -type l -ls
134320324    0 lrwxrwxrwx   1 root     root           17 Oct 17 18:54 /etc/mtab -> /proc/self/mounts
2835461    0 lrwxrwxrwx   1 root     root           55 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-telugu.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-telugu.conf
495591    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:56 /etc/fonts/conf.d/59-liberation-sans.conf -> /usr/share/fontconfig/conf.avail/59-liberation-sans.conf
2835462    0 lrwxrwxrwx   1 root     root           53 Oct 17 19:15 /etc/fonts/conf.d/65-wqy-microhei.conf -> /usr/share/fontconfig/conf.avail/65-wqy-microhei.conf
547486    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:57 /etc/fonts/conf.d/59-liberation-mono.conf -> /usr/share/fontconfig/conf.avail/59-liberation-mono.conf
2835467    0 lrwxrwxrwx   1 root     root           56 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-marathi.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-marathi.conf
553660    0 lrwxrwxrwx   1 root     root           59 Dec 19 07:07 /etc/fonts/conf.d/10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/
...

5.查找家目錄下,一天之內修改的文件

[root@DesktopComputer /tmp/test 14:34:32 ]$ find /root/ -ctime -1
/root/
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.config/gconf
/root/.xauthqWsH0f
[root@DesktopComputer /tmp/test 14:34:37 ]$ 

6.查找家目錄下,權限爲777的文件

[zhoupan@DesktopComputer ~ 14:38:00 ]$ find ~ -perm 777
/home/zhoupan/.config/google-chrome/SingletonLock
/home/zhoupan/.config/google-chrome/SingletonSocket
/home/zhoupan/.config/google-chrome/SingletonCookie
/home/zhoupan/.config/menus/gnome-applications-merged
...

7.查找/etc/下名爲passwd的文件,並顯示其內容

[root@DesktopComputer /tmp/test 15:51:32 ]$ find /etc/ -name passwd -exec cat {} \;
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

8.找到文件並刪除,刪的時候,需要用戶確認是否需要刪除

[zhoupan@DesktopComputer /tmp/test 16:27:25 ]$ ls
11.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:26 ]$ find -name '1*' -ok rm {} \;
< rm ... ./1.txt > ? n
< rm ... ./11.txt > ? y
[zhoupan@DesktopComputer /tmp/test 16:27:52 ]$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:53 ]$ 
發佈了129 篇原創文章 · 獲贊 203 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章