zoukankan      html  css  js  c++  java
  • linux文件查找(find,locate)

    文件查找:
    locate:
          非实时,模糊匹配,查找是根据全系统文件数据库进行的;
    # updatedb, 手动生成文件数据库
    速度快
     
    find:
          实时
          精确
          支持众多查找标准
          遍历指定目录中的所有文件完成查找,速度慢;
          
    find 查找路径 查找标准 查找到以后的处理运作
    查找路径:默认为当前目录
    查找标准:默认为指定路径下的所有文件
    处理运作:默认为显示
     
    匹配标准:
          -name 'FILENAME':对文件名作精确匹配
                文件名通配:
                      *:任意长度的任意字符
                      ?
                      []
          -iname 'FILENAME': 文件名匹配时不区分大小写
          -regex PATTERN:基于正则表达式进行文件名匹配
          
          -user USERNAME: 根据属主查找
          -group GROUPNAME: 根据属组查找
          
          -uid UID: 根据UID查找
          -gid GID: 根据GID查找
          
          -nouser:查找没有属主的文件
          -nogroup: 查找没有属组的文件
          
          -type
                f: 普通文件
                d
                c
                b
                l
                p
                s
          
          -size [+|-]
                #k
                #M
                #G
    /tmp目录,不是目录,并且还不能套接字类型的文件
    /tmp/test目录下,属主不是user1,也不是user2的文件;
     
          -mtime  修改时间
          -ctime  
          -atime  访问时间
                [+|-]#
          -mmin
          -cmin
          -amin
                [+|-]#
    解释-atime 5 表示距离此刻刚好五天的
        -atime +5  表示距离此刻大于五天都没访问过的
        -atime -5  表示距离此刻五天之内有访问过的
                
          -perm MODE:精确匹配
                /MODE: 任意一位匹配即满足条件
                -MODE: 文件权限能完全包含此MODE时才符合条件
                
                -644
                644: rw-r--r--
                755: rwxr-xr-x
                750: rwxr-x---
          find ./ -perl -001
    运作:
          -print: 显示
          -ls:类似ls -l的形式显示每一个文件的详细
          -ok COMMAND {} ; 每一次操作都需要用户确认
          -exec COMMAND {} ;
    运作例子:{}表示匹配到的内容
    [root@data-1-3 scripts]# find . -amin -30 -exec chmod u-w {} ;
    [root@data-1-3 scripts]# ll
    total 20
    -rwxr-xr-x 1 root root 280 Jan  7 11:55 1.sh
    -r--r--r-- 1 root root   0 Jan  8 08:03 a
    -rwxr-xr-x 1 root root 168 Jan  7 11:13 jiou_sum.sh
    -rwxr-xr-x 1 root root 261 Jan  8 03:30 sum.sh
    -rwxr-xr-x 1 root root 222 Jan  7 17:42 user01.sh
    -rwxr-xr-x 1 root root 489 Jan  7 18:24 user.sh
    [root@data-1-3 scripts]# find . -amin -30 -ok chmod u+w {} ;
    < chmod ... . > ? y
    < chmod ... ./a > ? y
    [root@data-1-3 scripts]# ll
    total 20
    -rwxr-xr-x 1 root root 280 Jan  7 11:55 1.sh
    -rw-r--r-- 1 root root   0 Jan  8 08:03 a
    -rwxr-xr-x 1 root root 168 Jan  7 11:13 jiou_sum.sh
    -rwxr-xr-x 1 root root 261 Jan  8 03:30 sum.sh
    -rwxr-xr-x 1 root root 222 Jan  7 17:42 user01.sh
    -rwxr-xr-x 1 root root 489 Jan  7 18:24 user.sh
    改文件名:
    [root@data-1-3 scripts]# find  -perm 644
    ./a
    [root@data-1-3 scripts]# find  -perm 644 -exec mv {} {}.new ;
    [root@data-1-3 scripts]# ll
    total 20
    -rwxr-xr-x 1 root root 280 Jan  7 11:55 1.sh
    -rw-r--r-- 1 root root   0 Jan  8 08:03 a.new
    -rwxr-xr-x 1 root root 168 Jan  7 11:13 jiou_sum.sh
    -rwxr-xr-x 1 root root 261 Jan  8 03:30 sum.sh
    -rwxr-xr-x 1 root root 222 Jan  7 17:42 user01.sh
    -rwxr-xr-x 1 root root 489 Jan  7 18:24 user.sh
     
    再一个例子:将大于1M的文件找出并追加到/tmp/etc.largesfile 
    [root@data-1-3 scripts]# find /etc/ -size +1M
    /etc/pki/tls/certs/ca-bundle.trust.crt
    /etc/selinux/targeted/policy/policy.24
    /etc/selinux/targeted/modules/active/policy.kern
    [root@data-1-3 scripts]# find /etc/ -size +1M |xargs >> /tmp/etc.largesfile
    [root@data-1-3 scripts]# cat /tmp/etc.largesfile
    /etc/pki/tls/certs/ca-bundle.trust.crt /etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/modules/active/policy.kern
    [root@data-1-3 scripts]# find /etc/ -size +1M -exec echo {} >> /tmp/etc.largesfile ;                    [root@data-1-3 scripts]# cat /tmp/etc.largesfile
    /etc/pki/tls/certs/ca-bundle.trust.crt /etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/modules/active/policy.kern
    /etc/pki/tls/certs/ca-bundle.trust.crt
    /etc/selinux/targeted/policy/policy.24
    /etc/selinux/targeted/modules/active/policy.kern
    ##############################################################
    [root@data-1-3 scripts]# stat a
      File: `a'
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 131939      Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2017-01-08 08:03:12.990058301 +0800
    Modify: 2017-01-08 08:03:07.122074618 +0800
    Change: 2017-01-08 08:03:12.990058301 +0800
     
    [root@data-1-3 scripts]# find -amin -5 -ls
    143757    4 drwxr-xr-x   2 root     root         4096 Jan  8 08:03 .
    131939    0 -rw-r--r--   1 root     root            0 Jan  8 08:03 ./a
    [root@data-1-3 scripts]# find -atime -5 -ls
    143757    4 drwxr-xr-x   2 root     root         4096 Jan  8 08:03 .
    131939    0 -rw-r--r--   1 root     root            0 Jan  8 08:03 ./a
    140442    4 -rwxr-xr-x   1 root     root          168 Jan  7 11:13 ./jiou_sum.sh
    140514    4 -rwxr-xr-x   1 root     root          222 Jan  7 17:42 ./user01.sh
    140515    4 -rwxr-xr-x   1 root     root          261 Jan  8 03:30 ./sum.sh
    140448    4 -rwxr-xr-x   1 root     root          280 Jan  7 11:55 ./1.sh
    140513    4 -rwxr-xr-x   1 root     root          489 Jan  7 18:24 ./user.sh
  • 相关阅读:
    转载 初学者必看——最简单最清晰的Struts2项目搭建流程
    五种常见设计模式
    第二则java读取excel文件代码
    使用Maven运行Java main的3种方式使用Maven运行Java main的3种方式
    C++模式学习------策略模式
    C++模式学习------工厂模式
    人生辣么多的谎言,没必要一个个试一下
    常用函数说明
    python 查看与更换工作目录
    unix常用命令记录
  • 原文地址:https://www.cnblogs.com/shanhua-fu/p/6902525.html
Copyright © 2011-2022 走看看