zoukankan      html  css  js  c++  java
  • CentOS 文件搜索find

    1、文件搜索,内置的的命令是find

    用法: find [查找路径] 寻找条件 操作
    
    默认路径为当前目录;默认表达式为 -print
    

    2、主要参数:

    -name  匹配名称 
    -perm  匹配权限(mode为完全匹配,-mode 为包含即可) 
    -user   匹配所有者 
    -group  匹配所有组 
    -mtime -n +n  匹配修改内容的时间(-n指 n天以内,+n指 n天以前) 
    -atime -n +n  匹配访问文件的时间(-n指 n天以内,+n指 n天以前) 
    -ctime -n +n  匹配修改文件权限的时间(-n指 n天以内,+n指 n天以前) 
    -nouser  匹配无所有者的文件 
    -nogroup  匹配无所有组的文件 
    -newer f1 !f2  匹配比文件 f1新但比 f2旧的文件 
    --type b/d/c/p/l/f 匹配文件类型(后面的字母参数依次表示块设备、目录、字符道、链接文件、文本文件) 
    -size 匹配文件的大小(+50KB 为查找超过 50KB 的文件,而-50KB小于50KB的文件) 
    -prune  忽略某个目录 
    -exec …… {};  find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;,注意{ }和;之间的空格,{}表示查找出来的没一个文件,结尾必须是";"

    3、按名字查找

    在/opt目录下查找elasticsearch的配置文件
    [root@bigdata-senior01 ~]# find /opt -name "elastic*.yml" /opt/elasticsearch-6.5.1/config/elasticsearch.yml

    查找过程中出现“权限不够”的提示:
    [xu.dm@bigdata-senior01 ~]$ find /opt -name "elastic*.yml"
    find: ‘/opt/modules/hadoop-3.1.0/logs/userlogs/application_1527345950418_0001_DEL_1527434392341’: 权限不够
    find: ‘/opt/data/tmp/dfs/data’: 权限不够
    find: ‘/opt/data/tmp/nm-local-dir/usercache_DEL_1527434391968/root’: 权限不够
    find: ‘/opt/data/tmp/nm-local-dir/nmPrivate’: 权限不够
    find: ‘/opt/data/tmp/nm-local-dir/usercache/hadoop’: 权限不够
    /opt/elasticsearch-6.5.1/config/elasticsearch.yml

    避免因为权限不够的提示,把查找错误提示转移到特定的目录中去。
    [xu.dm@bigdata-senior01 ~]$ find /opt -name "elastic*.yml" 2> /dev/null
    /opt/elasticsearch-6.5.1/config/elasticsearch.yml

    按名字忽略大小写搜索
    [root@vmstation ~]# find /etc -iname "SELINUX"
    /etc/sysconfig/selinux
    /etc/selinux


    4、按文件大小查找

    查找/home下大于10M的文件
    [root@bigdata-senior01 ~]# find /home -size +10M /home/data/kafka_2.12-2.0.0.tgz
    查找小于1k的文件
    [root@bigdata-senior01 ~]# find /home -size -1k

    5、与,或条件,and,or

    [root@bigdata-senior01 ~]# find /opt -name "*yml" -and -name "elastic*"
    /opt/elasticsearch-6.5.1/config/elasticsearch.yml

    find / -user fred -or -user georg

    6、其他

      find / -amin -10 # 查找在系统中最后10分钟访问的文件
      find / -atime -2 # 查找在系统中最后48小时访问的文件
      find / -empty # 查找在系统中为空的文件或者文件夹
      find / -group cat # 查找在系统中属于 groupcat的文件
      find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
      find / -mtime -1 #查找在系统中最后24小时里修改过的文件
      find / -nouser #查找在系统中属于作废用户的文件
      find / -user fred #查找在系统中属于FRED这个用户的文件

    在find命令中还可以使用“非”的关系来查找文件,如果我们要在/tmp目录中查找所有不属于panda的文件,使用一个简单的
      find /tmp ! -user panda

      查找并显示文件的方法
    find / -name "httpd.conf" -ls

    find   .   -perm -007   -exec ls -l {} ;   #查所有用户都可读写执行的文件同-perm 777
    find   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   #查以两个小写字母和两个数字开头的txt文件
    find   /home   -uid   +501                  列出/home目录内用户的识别码大于501的文件或目录
    find   /home   -gid 501                   列出/home内组id为501的文件或目录

    7、exec参数,{}表示查找出来的没一个文件,结尾必须是";"

    在/opt目录下查找文件,并拷贝到但前目录下
    [root@bigdata-senior01 ~]# find /opt -name "elastic*.yml" -exec cp {} ./ ;
    找到文件并用ls -lh显示
    [root@bigdata-senior01 ~]# find /opt -name "*.conf" -exec ls -lh {} ; -rw-r--r--. 1 hadoop hadoop 1.1K 3月 30 2018 /opt/modules/hadoop-3.1.0/share/hadoop/yarn/yarn-service-examples/httpd/httpd-proxy.conf -rw-r--r--. 1 hadoop hadoop 1009 3月 30 2018 /opt/modules/hadoop-3.1.0/share/hadoop/yarn/yarn-service-examples/httpd-no-dns/httpd-proxy-no-dns.conf
  • 相关阅读:
    Instruments Tutorial for iOS: How To Debug Memory Leaks
    How to Use Instruments in Xcode
    Demystifying iOS Application Crash Logs
    “iOS 推送通知”详解:从创建到设置到运行
    推送通知iOS客户端编写实现及推送服务器端编写
    cocos2d-iphone 与 UI组件
    ScrollLayer
    TexturePacker
    mybatis的配置
    spring+redis
  • 原文地址:https://www.cnblogs.com/asker009/p/10236088.html
Copyright © 2011-2022 走看看