寫在前面,命令總覽:
文件名:-name -iname glob 從屬關系: -user -group -uid –gid -nouser -nogroup
按類型:-type [] ,f,d,l,b,c,p,s 組合測試:邏輯與或非-a -o ,-not,! 大?。?size
時間:-atime –mtime -amin 權限:-perm 處理動作:-print -ls -delete -fls -ok -exec
find命令詳解
一、簡介
find是一款文件查找工具,非常適合查找計算機上的文件。
二、與locate優缺點比較
locate:
優點:查找速度快,自動更新數據庫
缺點:依賴于數據庫,非實時查找,結果非精確,模糊查找
find:
優點:精確匹配,實時查找,通過遍歷指定路徑文件系統層級結構完成文件查找
缺點:查詢速度較慢
find用法:
find [OPTIONS] [查找起始路徑] [查找條件] [處理動作]
查找起始路徑:指定具體搜索目標起始路徑;默認為當前目錄;
查找條件:指定的查找標準,可以根據文件名、大小、類型、從屬關系、權限等等標準進行;
默認為找出指定路徑下的所有文件;
處理動作:對符合查找條件的文件做出的操作,例如刪除等操作;默認為輸出至標準輸出;
查找條件:
測試:結果常為布爾型(真假)
根據文件名查找:
-name ”pattern“
-iname “pattern”, ignorecase 忽略大小寫
[root@yph7 tmp]# find /tmp -iname "gentoo"
/tmp/GENtoo
glob支持文件名通配
*, ?, [], [^]
[root@yph7 tmp]# find /tmp -iname "gen*"
/tmp/GENtoo
/tmp/gentext
根據文件從屬關系查找:
-user USERNAME:查找屬主指定用戶的所有文件;
[root@yph7 tmp]# find /tmp -user breeze –ls ——– -ls指查看詳細屬性
3552 8 -rw-rw-r– 1 breeze breeze 19 12月 16 06:13 /tmp/a.breeze
67202546 4 -rw——- 2 breeze root 2800 12月 18 06:17 /tmp/dir.root/passwd.root
67202546 4 -rw——- 2 breeze root 2800 12月 18 06:17 /tmp/dir.root/passwd.root.link
-group GRPNAME:查找屬組指定組的所有文件;
[root@yph7 tmp]# find /tmp -group apache -ls
33554819 0 drwxrwsr-t 2 root apache 51 12月 16 05:42 /tmp/text
33554841 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
-uid UID:查找屬主指定的UID的所有文件;
[root@yph7 tmp]# id -u hadoop
2051
[root@yph7 tmp]# find /tmp -uid 2051 -ls
33554841 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
33554840 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:34 /tmp/text/c.hadoop
-gid GID:查找屬組指定的GID的所有文件;
[root@yph7 tmp]# id -g breeze
1004
[root@yph7 tmp]# find ./ -gid 1004 -ls
3552 8 -rw-rw-r– 1 breeze breeze 19 12月 16 06:13 ./a.breeze
-nouser:查找沒有屬主的文件;
[root@yph7 tmp]# find ./ -nouser -ls
283 0 -rw-r–r– 1 2003 hadoop 0 12月 20 20:06 ./a.mysql1
-nogroup:查找沒有屬組的文件;
[root@yph7 tmp]# find /home -nogroup -ls
255 0 drwx—— 3 breeze 4002 89 12月 13 05:52 /home/fedora
根據文件的類型查找:
-type TYPE:
f: 普通文件
[root@yph7 tmp]# find ./ -type f -ls
282 0 -rw-r–r– 1 root root 0 12月 20 19:48 ./gentext
283 0 -rw-r–r– 1 2003 hadoop 0 12月 20 20:06 ./a.mysql1
d: 目錄文件
[root@yph7 tmp]# find /tmp -type d -ls
133 4 drwxrwxrwt 11 root root 4096 12月 20 20:06 /tmp
67156266 0 drwxrwxrwt 2 root root 6 12月 8 21:20 /tmp/.X11-unix
l:符號鏈接文件
[root@yph7 tmp]# find /tmp -type l -ls
67202555 0 lrwxrwxrwx 1 root root 11 12月 18 06:21 /tmp/ passwd.symbolink -> passwd.root
b:塊設備 文件
[root@yph7 tmp]# find /dev -type b -ls
10821 0 brw-rw—- 1 root disk 8, 7 12月 20 18:02 /dev/sda7
10820 0 brw-rw—- 1 root disk 8, 6 12月 20 18:02 /dev/sda6
c:字符設備文件
p:管道文件
s:套接字文件
組合測試:
-a,and邏輯與關系,為系統默認
[root@yph7 tmp]# find -name "pass*" -a -user flimmer –ls ——–必須同時滿足兩個條件
274 4 -rw-r–r– 1 flimmer flimmer 2800 12月 17 22:18 ./passwd.flimmer
-o,or邏輯或關系
[root@yph7 tmp]# find /tmp \( -user flimmer -o -name "pass*" \) -ls
270 4 -rw-r–r– 1 root root 2800 12月 16 18:43 /tmp/passwd
273 0 -rw-r–r– 1 flimmer flimmer 0 12月 17 22:16 /tmp/a.flimmer
-not或者!:邏輯非關系
[root@yph7 tmp]# find /tmp -not -user root -ls
33554841 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
33554831 4 -rw-rw-r– 1 gentoo apache 95 12月 17 06:45 /tmp/text/c.gentoo
練習:
1、找出/tmp目錄下文件名中不包含fstab字符串的文件;
[root@yph7 tmp]# find /tmp -not -name "*passwd*"
2、找出/tmp目錄下屬主為非root,而且文件名不包含fstab字符串的文件;
[root@yph7 tmp]# find /tmp -not -user root -a -not -name "*fstab*" -ls
[root@yph7 tmp]# find -not \( -user root -o -name "*fstab*" \) –ls
[root@yph7 tmp]# find ! \( -user root -o -name "*passwd*" \) –ls
根據文件大小查找:
-size[+|-]#UNIT 常用單位:k(小寫), M, G
-5k:[0,5) ——–小于5k ——個人理解
5k:(4,5] ——–5K的文件,大于4K,小于等于5K
+5k:(5,+oo) ———–大于5k的文件
[root@yph7 tmp]# find /tmp -size -5k ———–小于5k的文件
/tmp
/tmp/.X11-unix
/tmp/.ICE-unix
以天為單位查找:
-3 :過去的3天內,不包括等于第3天
3:過去的第二天到過去的第三天之間
+3 : 過去的第四天及第四天之前的時間,至少已經三天沒訪問了
-atime[+|-]#:最近一次訪問時間
[root@yph7 text]# find ./ -atime -3 ———查看三天內訪問過的文件
./ ————沒有三天內訪問過的文件
[root@yph7 text]# ll
總用量 4
-rw-rw-r–. 1 gentoo apache 95 12月 17 06:45 c.gentoo
-rw-rw-r–. 1 hadoop apache 0 12月 16 05:34 c.hadoop
[root@yph7 text]# cat c.gentoo &> /dev/null ——-現在就訪問一次
[root@yph7 text]# find ./ -atime -3
./
./c.gentoo [root@yph7 text]# cat ./b.hadoop ——–發現就找到了
[root@yph7 text]# stat ./c.gentoo ————用stat查看一下c.gentoo的時間戳
文件:"./c.gentoo"
大?。?/span>95 塊:8 IO 塊:4096 普通文件
設備:802h/2050d Inode:33554831 硬鏈接:1
權限:(0664/-rw-rw-r–) Uid:( 4006/ gentoo) Gid:( 2011/ apache)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2015-12-20 22:14:40.159540734 +0800
最近更改:2015-12-17 06:45:08.922153037 +0800
最近改動:2015-12-17 06:45:08.922153037 +0800
-mtime:最近一次修改文件內容時間
-ctime: 最近一次修改元數據的時間。權限屬性等,
[root@yph7 text]# find ./ -ctime -1 ———–最近一天內修改過元素據的文件,發現沒有
./
[root@yph7 text]# chmod o+w c.gentoo ——-修改下元素據,更改下權限
[root@yph7 text]# find ./ -ctime -1
./
./c.gentoo ————就能找到了
以分鐘為單位查找 用法與根據天查找相同
-amin:最近一次訪問時間,以分鐘為單位
[root@yph7 tmp]# find ./ -amin -3 ——-查找3分鐘內訪問過的文件,發現沒有
[root@yph7 tmp]# cat ./c.root > /dev/null ——–現在訪問一次
[root@yph7 tmp]# find ./ -amin -3
./c.root ————就能查找到了
-mmin:最近一次修改內容時間,分鐘為單位
[root@yph7 tmp]# find ./ -mmin +3 —查找三分鐘之前修改過內容的文件,發現所有文件都列了出來
./ —–因為三分鐘內沒有修改過文件內容,所有文件都符合條件
./.X11-unix
./.ICE-unix
./.XIM-unix
………………………………………
-cmin:最近一次修改元數據時間,分鐘為單位,權限屬性等
根據權限查找
-perm [/|-]mode
mode:精確權限匹配
[root@yph7 text]# find ./ -perm 664 –ls ——–查找權限為664的文件
33554840 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:34 ./c.hadoop
/mode:任何一類用戶(u,g,o)的權限中的任何一位(r,w,x)符合條件即滿足;只需有任何一個交集
9位權限之間存在“或”關系只需滿足其中任何一個;比如一個權限為rw-r—-的文件,為“-”的位就忽略不考慮,只考慮出現”r或w或x”這三個字母的位,即只認為有r,w,r,權限,如果模式為/034,即—-wx—,文件權限中有一個w,模式中也有一個w這就匹配到了。如果為/100,即x——–,只有一個x,但文件權限例一個x都沒有,就不能匹配到。
[root@yph7 text]# chmod 640 c.gentoo
[root@yph7 text]# find ./ -perm /034 -ls
33554819 0 drwxrwsr-t 2 root apache 36 12月 20 22:13 ./
33554840 0 -rw-rw-r– 1 hadoop apache 0 12月 16 05:34 ./c.hadoop
[root@yph7 text]# find ./ -perm /100
./
[root@yph7 text]#
-mode:每一類用戶(u,g,o)的權限中的每一位(r,w,x)同時符合條件即滿足;必須完全包括模式
9位權限之間存在“與”關系;即u,g,o三類用戶的權限都必須多與或等于模式給出的對應的權限。比如A文件權限為rw-r—–,B文件權限為rw-rw-r–,模式rw-rw——滿足B不能滿足A,r—r—–能同時滿足AB。要想滿足A就必須少與或等于rw-r—–,要滿足B就必須少于或等于rw-rw-r–.
[root@yph7 text]# ll
總用量 4
-rw-r—–. 1 gentoo apache 95 12月 17 06:45 c.gentoo
-rw-rw-r–. 1 hadoop apache 0 12月 16 05:34 c.hadoop
[root@yph7 text]# find ./ -perm -444
./
./c.hadoop
[root@yph7 text]# find ./ -perm -400
./
./c.gentoo
./c.hadoop
[root@yph7 text]# find ./ -perm -440
./
./c.gentoo
./c.hadoop
[root@yph7 text]# find ./ -perm -664
./
./c.hadoop
[root@yph7 text]# find ./ -perm -663
[root@yph7 text]# find ./ -perm -640
./
./c.gentoo
./c.hadoop
處理動作:
-print:輸出至標準輸出;默認的動作;
-ls:類似于對查找到的文件執行“ls -l”命令,輸出文件的詳細信息;
-delete:刪除查找到的文件;
[root@yph7 text]# ll
總用量 4
-rw-r—–. 1 gentoo apache 95 12月 17 06:45 c.gentoo
-rw-rw-r–. 1 hadoop apache 0 12月 16 05:34 c.hadoop
[root@yph7 text]# find ./ -perm -444 -delete
find: 無法刪除 ‘./’: 無效的參數
[root@yph7 text]# ll
總用量 4
-rw-r—–. 1 gentoo apache 95 12月 17 06:45 c.gentoo —-c.hadoop已經被刪掉了
-fls /PATH/TO/SOMEFILE:把查找到的所有文件的長格式信息保存至指定文件中;
[root@yph7 tmp]# cat c.root
[root@yph7 tmp]# find -name "pass*" -fls ./c.root
[root@yph7 tmp]# cat c.root
270 4 -rw-r–r– 1 root root 2800 12月 16 18:43 ./passwd
274 4 -rw-r–r– 1 flimmer flimmer 2800 12月 17 22:18 ./passwd.flimmer
67202546 4 -rw——- 2 breeze root 2800 12月 18 06:17 ./dir.root/passwd.root
67202546 4 -rw——- 2 breeze root 2800 12月 18 06:17 ./dir.root/passwd.root.link
-ok COMMAND {} \; :對查找到的每個文件執行由COMMAND表示的命令;每次操作都由用戶進行確認;
-exec COMMAND {} \; :對查找到的每個文件執行由COMMAND表示的命令;
注意:find傳遞查找到的文件路徑至后面的命令時,是先查找出所有符合條件的文件路徑,并一次性傳遞給后面的命令;
但是有些命令不能接受過長的參數,此時命令執行會失?。涣硪环N方式可規避此問題:
find | xargs COMMAND
[root@yph7 text]# find ./ -perm -440 -ls -ok mv {} {}.bak \; ——–會詢問是否要進行
33554819 0 drwxrwsr-t 2 root apache 21 12月 20 23:56 ./
< mv … ./ > ? y
mv: 無法將"./" 移動至"./.bak": 設備或資源忙
33554831 4 -rw-r—– 1 gentoo apache 95 12月 17 06:45 ./c.gentoo
< mv … ./c.gentoo > ? y
[root@yph7 text]# ll
總用量 4
-rw-r—–. 1 gentoo apache 95 12月 17 06:45 c.gentoo.bak
[root@yph7 text]# find ./ -perm -440 -ls
33554819 0 drwxrwsr-t 2 root apache 21 12月 20 23:22 ./
33554831 4 -rw-r—– 1 gentoo apache 95 12月 17 06:45 ./c.gentoo
[root@yph7 text]# find ./ -perm -440 -ls -exec mv {} {}.bak \; ——-不會詢問你,直接進行
[root@yph7 text]# ll
總用量 4
-rw-r—–. 1 gentoo apache 95 12月 17 06:45 c.gentoo.bak
[root@yph7 tmp]# find /etc -name "issue*" -exec cat {} &> /tmp/c.root \;
[root@yph7 tmp]# cat /tmp/c.root
\S
Kernel \r on an \m
Mage Education Learning Services
http://www.magedu.com
\S
Kernel \r on an \m
練習:
1、查找/usr目錄下不屬于root, bin或hadoop的所有文件或目錄;用兩種方法;
[root@yph7 tmp]# find /usr -not -user root -a -not -user bin -a -not -user hadoop
[root@yph7 tmp]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
2、查找/etc目錄下最近一周內其內容修改過,且屬主不是root用戶也不是hadoop用戶的文件或目錄;
[root@yph7 tmp]# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls
[root@yph7 tmp]# find /etc -mtime -7 -a -not -user root -a -not -user hadoop -ls
3、查找當前系統上沒有屬或屬組,且最近一周內曾被訪問過的文件或目錄;
[root@yph7 init.d]# find / \( -nouser -o -nogroup \) -a -atime -7 –ls
—必須加括號,否則-o與-a順序不清楚
283 0 -rw-r–r– 1 2003 hadoop 0 12月 20 20:06 /tmp/a.mysql1
255 0 drwx—— 3 breeze 4002 89 12月 13 05:52 /home/fedora
4、查找/etc目錄下大于1M且類型為普通文件的所有文件;
[root@yph7 text]# find /etc -size +1M -exec ls -lh {} \;
-r–r–r–. 1 root root 6.1M 12月 8 19:47 /etc/udev/hwdb.bin
-rw-r–r–. 1 root root 3.7M 3月 6 2015 /etc/selinux/targeted/policy/policy.29
5、查找/etc目錄下所有用戶都沒有寫權限的文件;
[root@yph7 text]# find /etc -not -perm /222 -type f -ls
6、查找/etc目錄至少有一類用戶沒有執行權限的文件;
[root@yph7 text]# find /etc -not -perm -111 -type f –ls
67198001 4 -rw-r–r– 1 root root 19 12月 8 20:43 /etc/locale.conf
67198008 12 -rw-r–r– 1 root root 12288 12月 8 20:45 /etc/aliases.db
67202533 4 -rw-r–r– 1 root root 17 12月 9 21:09 /etc/hostname
7、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的所有文件;
[root@yph7 tmp]# find /etc -perm -113 -type f -ls
[root@yph7 text]# find /etc/init.d -perm -111 -a -perm -002 -type f -ls
原創文章,作者:flivfox,如若轉載,請注明出處:http://www.www58058.com/10273