find命令詳解

寫在前面,命令總覽:

文件名:-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

根據文件從屬關係

-userUSERNAME找屬主指定用的所有文件;

[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

-groupGRPNAME找屬指定的所有文件;

[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

-uidUID找屬主指定的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@yph7text]# 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@yph7text]# cat c.gentoo &> /dev/null  -------在就訪問一次

 [root@yph7text]# find ./ -atime -3

./

./c.gentoo [root@yph7 text]# cat ./b.hadoop --------發現就找到了

 

[root@yph7 text]# stat ./c.gentoo  ------------用stat看一下c.gentoo的時間

  文件:"./c.gentoo"

  大小: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    0drwxrwsr-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-----. 1gentoo apache 95 12月 17 06:45 c.gentoo

-rw-rw-r--. 1hadoop 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

-okCOMMAND {} \;   對查找到的每個文件行由COMMAND表示的命令;每次操作都由用戶進行確

-execCOMMAND {} \;  對查找到的每個文件行由COMMAND表示的命令;

注意:find傳遞查找到的文件路徑至後面的命令,是先找出所有符合條件的文件路徑,並一次性傳遞給後面的命令;

但是有些命令不能接受過長的參數,此命令行會失;另一種方式可避此問題

find | xargsCOMMAND

 

[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-----. 1gentoo 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-----. 1gentoo 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 EducationLearning 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--. 1root root 6.1M 12月  8 19:47 /etc/udev/hwdb.bin

-rw-r--r--. 1root 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章