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

根据文件从属关系查找:

-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"

  大小:95            块:8          IO 块:4096   普通文件

设备:802h/2050d Inode33554831    硬链接: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位权限之间存在“与”关系;ugo三类用户的权限都必须多与或等于模式给出的对应的权限。比如A文件权限为rw-r—–,B文件权限为rw-rw-r–,模式rw-rw——满足B不能满足Ar—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传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;

但是有些命令不能接受过长的参数,此时命令执行会失败;另一种方式可规避此问题:

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.178linux.com/10273

(0)
上一篇 2016-02-14 10:10
下一篇 2016-02-14 10:12

相关推荐

  • 使用 nice、cpulimit 和 cgroups 限制 cpu 占用率

    Linux内核是一名了不起的马戏表演者,它在进程和系统资源间小心地玩着杂耍,并保持系统的正常运转。 同时,内核也很公正:它将资源公平地分配给各个进程。 但是,如果你需要给一个重要进程提高优先级时,该怎么做呢? 或者是,如何降低一个进程的优先级? 又或者,如何限制一组进程所使用的资源呢? 答案是需要由用户来为内核指定进程的优先级 大部分进程启动时的优先级是相同…

    Linux干货 2015-02-14
  • 精解局域网访问及共享(三)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/155109    上一节中的2种情况都是默认的简单共享,下面我们着重分析一下高级共享方式下的各种访问情况。毕竟这种方式用的很普遍,而且在权限…

    Linux干货 2015-03-25
  • 初来乍到

    坐上了去往北方的火车,我不知道自己为什么会颤抖,也许是耳朵里那首汪峰的《北京,北京》震撼到了我,接着满脑子便是灯红酒绿的大街道和浮华的高楼大厦,我幻想着有一天能在这样的大城市中闯出一片天。梦醒了 ! 30个小时的路程确实是让我满脑子都是未来的自己。 对于我这个从来没有见过世面的人来说,第一次来到北京这座一线大城市,内心充满着无比的欣喜和激动,但更多的还是那份…

    Linux干货 2018-03-26
  • 描述计算机的组成及其功能

    CPU:计算器,控制器,寄存器内存输入输出设备

    Linux干货 2016-08-15
  • Docker 之初次体验

    一、Docker 简介  lxc linux container,openvz  容器中各虚拟机只有一个内核,而是多个用户空间  在库中完成虚拟化,比如wine 或者在windows中运行bash  在应用程序的运行级别提供虚拟化,比如jvm   pstree , pid 为1 的进程  …

    Linux干货 2017-02-24
  • lamp部署及编译安装

    lamp 资源类型: 静态资源:原始形式与响应给客户端的结果一致; 动态资源:原始形式通常为程序文件(为某种编程语言开发),需要运行后将生成的结果展示给客户端;如果请求的资源不存在那么结果就会重定向至指定的文件中 客户端技术:javascript 服务端技术:php, jsp, … CGI:Common Gateway Interfa…

    Linux干货 2016-10-19