文件查找详解

查找命令有locate和find

一、locate

    根据全系统文件数据库进行查找,并非实时查找。按计划任务会自动将本机的文件收集到文件数据库中,非精确查找。

    手动生成文件数据库,updatedb,执行速度慢,查找速度快

二、find

    实时、精确、遍历、查找速度慢、支持众多查找类型

    find 查找路径 查找标准 查找的处理动作

    查找路径:默认为当前路径

    查找标准:默认为路径下所有文件

    查找的处理动作:默认为显示

    匹配标准:

        -name 'filename':根据文件名作精确查找并区分大小写,支持文件名通配*,?,[]       

         [root@station01 ~]# find /etc -name 'passwd'

        -iname 'filename':文件名匹配不区分大小写

        -regex PATTERN:基于正则表达式进行文件名匹配

        -user USERNAME:根据文件的属主进行查找

        -group:GROUPNAME:根据文件的属组进行查找

        -uid:根据uid查找

        -gid:根据gid查找

        -nouser:查找没有属主的文件

        -nogroup:查找没有属组的文件

        -type TYPE:根据文件类型查找

            f:普通文件

            d:目录

            c:字符

            b:块设备

            l:连接文件

            p:管道

            s:套接字

        -size:文件大小查找

            M

            K

            G

            +-表示范围

        组合条件

            -a 与

            -o    或

            -not 非

        找出/tmp非普通文件

 [root@station01 ~]# find /tmp -not -type f

        找出不是用户1的又不是用户2的文件

    1)

 [root@station01 tmp]# find ./ -not -user user1 -a -not -user user2

     2)

 [root@station01 tmp]# find ./ -not \( -user user1 -o -user user2 \)

    根据文件的时间戳查找文件

    -mtime [+|-]#:修改时间天数

    -ctime [+|-]#:改变时间天数

    -atime [+|-]#:访问时间天数

        #:到此刻刚好#天

        -#:最近#天

        +#:至少有#天

      -mmin[+|-]#:修改时间分钟

     -cmin[+|-]#:改变时间分钟

     -amin [+|-]#:访问时间分钟

    根据文件权限查找

    -perm mode 权限精确查找

    -perm -mode每一位权限必须全部匹配

    -perm /mode有任何一位匹配都可以

    

    动作

        -print

        -ls

        -ok command {} \;每一次操作都需要用户确认

        -exec command {} \; 其中{}号表示引用匹配到的文件名

 [root@station01 tmp]# find ./ -perm 004 -exec chmod o-r {} \;

        文件改名

 [root@station01 tmp]# find ./ -perm 000 -exec mv {} {}.new \;
 [root@station01 tmp]# find /root/ -name "*.sh" -a -perm -111 -exec chmod o-x {} \;

    1、查找/var目录下属主为root并且属组为mail的所有文件

    [root@station01 tmp]# find /var/ -user root -group mail -ls

    2、查找/var目录下不属于root,bin或student用户的文件

    [root@station01 tmp]# find ./ -not \( -user user1 -o -user root -o -user bin \)

    3、查找/etc目录下最近一周修改过并且不属于root及user2用户的文件

    [root@station01 tmp]# find /etc/ -mtime -7 -not \( -user root -o -user user2 \)

    4、查找当前系统上没有属主和属组且最近一天内曾被访问过的文件,并将其属主属组修改为root

    [root@station01 tmp]# find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;

    5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/largefiles中

    [root@station01 tmp]# find /etc/ -size +1M >>/tmp/largefiles

    6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息

    [root@station01 tmp]# find /etc/ -not -perm /222  -ls
     [root@station01 tmp]# find /etc/ -size +1M -exec echo {} >>/tmp/largefiles \;
    [root@station01 tmp]# find /etc/ -size +1M | xargs echo {} >>/tmp/largefiles \;

 

GB2312

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