码迷,mamicode.com
首页 > 系统相关 > 详细

linux find命令

时间:2016-03-17 02:14:11      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:find命令

find命令是linux系统下常用的文件查找工具,还可以对查找到的文件执行相应的操作。

格式

find [查找的路径] [查找条件] [处理动作]

默认行为:

                 查找路径:默认不指定为当前路径

                 查找条件:默认为当前路径下所有文件

                 处理动作:默认为print

注意:如果查找条件是目录,需要带上/



查找条件:

    1、- name "文件名" ,支持使用文件名通配。

对于文件名还有另一个选项,- iname 该选项的作用和 -name类似,但查找时不区分大小写

# find . -name "*.sh"
./useradd.sh
./while.sh

     2、-user UserName,根据文件属主查找。

# find . -user tom -ls
523272    0 -rw-rw-r--   1 tom      tom             0 Mar 16 14:14 ./filetest

    3、-group GroupName , 根据文件属组查找

# find . -group tom -ls
523272    0 -rw-rw-r--   1 tom      tom             0 Mar 16 14:14 ./filetest

    4、-uid  UID , 根据用户的UID来查找

         -gid  GID, 根据用户的GID来查找

        有时候用户删除了,但该用户的文件还在,找出这些文件只能根据UID或GID来查找

[root@zibbix tmp]# find /tmp -uid 503  -ls
523272    0 -rw-rw-r--   1 503      503             0 Mar 16 14:14 /tmp/filetest

  

    5、- type 根据文件类型来查找

            f:普通文件

            d:目录

            l:符号链接

            b:块设备

            p:命名管道

            s:套接字

            c:块设备

# find /tmp -type d -ls
523265    4 drwxrwxrwt   4 root     root         4096 Mar 16 14:25 /tmp
523274    4 drwxr-xr-x   2 root     root         4096 Mar 16 14:25 /tmp/dir1
655081    4 drwxrwxrwt   2 root     root         4096 Feb 28 10:14 /tmp/.ICE-unix


    6、- size :根据文件大小查找

 -size [+|-] #Unit

  用法:

    -size +2M  \\ 查找大于2M的文件
    -size -2M   \\ 查找小于2M的文件(#-1M,查找1M以下的文件)
    -size 2M    \\ 精确匹配2M的文件



    7、根据时间戳来查找

        (a)、 以天为单位(time)

                - atime [+|-] #,#为天

                    +: 表示(#+1)天之外被访问过;
                    -: 表示#天之内被访问过;
                    无符号:表示短于(#+1)> x >=#天的时间段被访问过;

技术分享







      

         (b)、 以分钟为单位(min)

                  -amin [+|-]#
                  -mmin
                  -cmin


    8、根据权限查找,-perm [+|-]  MODE

             MODE:精确匹配

            +MODE:常用于地查找某类用户的特定权限是否存在;

                               --- --- --- 只要属主、属组、其它用户、的任意权限位匹配到即可   

            -MODE:每类用户的指定要检查的权限位都匹配;   

假如有一个644的权限的文件

root@zibbix tmp]# ls -l
-rw-r--r--. 1 root root 0 Mar 16 15:33 test1   \\ 644
-rw-------. 1 root root 0 Mar 16 15:40 test2   \\ 600
-rw-r-----. 1 root root 0 Mar 16 15:40 test3   \\ 640

精确匹配,必须所有的位都匹配到才可以。

# find /tmp -perm 644 -ls
523272    0 -rw-r--r--   1 root     root            0 Mar 16 15:33 /tmp/test1

+MODE,只要有一个权限匹配到即可

[root@zibbix tmp]# find /tmp -perm +006 -type f -ls
523272    0 -rw-r--r--   1 root     root            0 Mar 16 15:33 /tmp/test1
# find /tmp -perm +666 -type f -ls
523272    0 -rw-r--r--   1 root     root            0 Mar 16 15:33 /tmp/test1
523273    0 -rw-------   1 root     root            0 Mar 16 15:40 /tmp/test2
523274    0 -rw-r-----   1 root     root            0 Mar 16 15:40 /tmp/test3

-MODE,每一个位的权限都要被检查,必须精确匹配到。

# find /tmp -perm -240 -type f -ls
523272    0 -rw-r--r--   1 root     root            0 Mar 16 15:33 /tmp/test1
523274    0 -rw-r-----   1 root     root            0 Mar 16 15:40 /tmp/test3
# find /tmp -perm -444 -type f -ls
523272    0 -rw-r--r--   1 root     root            0 Mar 16 15:33 /tmp/test1



组合条件:
            -a: 与,同时满足,多个条件,默认就是-a选项
            -o: 或,
            -not, !:非,取反

# find /tmp -name "*.doc" -a -perm 640 -ls
523274    0 -rw-r-----   1 root     root            0 Mar 16 15:40 /tmp/test.doc

            非A,并且 非B  = 非(A或B)

# find /tmp -not \( -user tom -o -name "*.txt" \) -ls
523273    0 -rw-------   1 root     root            0 Mar 16 15:40 /tmp/test.xml

# find /tmp -not -user tom -a -not -name "*.txt" -ls
523273    0 -rw-------   1 root     root            0 Mar 16 15:40 /tmp/test.xml

          

            非A,或 非B  =   非(A且B)
            find /tmp -not -user hadoop   -o  -not -name "*.txt" 
            find /tmp -not \( -user hadoop -a -name "*.txt" \)


————

处理动作:
        -print:打印在标准输出上;
        -ls:以长格式输出各文件信息;
        -exec COMMAND {} \; :对查找到的文件执行指定的命令
        -ok COMMAND {} \; : 交互式的-exec;

            find把查找到的所有文件一次性地传递给-exec所指定的命令

        find | xargs COMMAND
            find /tmp -perm -003 -type f | xargs chmod o-wx
                像这样xargs是无法完全替代 -exec 如实面文件改名操作
            find /tmp/ -iname *.doc -exec mv {} {}x \;  把原有以doc结尾的文件改为docx结尾



练习:
1、查找/var/目录属主为root且属组为mail的所有文件;
# find /var/ -user root -a -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、查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件;
# find / \( -nouser -o -nogroup \) -a -atime -30

5、查找/etc/目录下大于1M且类型为普通文件的所有文件;
# find /etc/ -size +1M -a -type f


6、查找/etc/目录所有用户都没有写权限的文件;
# find /etc/ -not -perm +222
    所有都没有:相反:任何一个有
    所有都有:相反:至少有一个没有

7、查找/etc/目录下至少有一类用户没有写权限;
# find /etc/ -not -perm -222

8、查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件;
# find /etc/init.d/  -perm -113



find /tmp -iname "*.doc" -exec mv {} {}x \;
find与xargs
在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。










本文出自 “菜鸟日志” 博客,请务必保留此出处http://zkxfoo.blog.51cto.com/1605971/1751888

linux find命令

标签:find命令

原文地址:http://zkxfoo.blog.51cto.com/1605971/1751888

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!