zoukankan      html  css  js  c++  java
  • Linux基础文件查找

    一、文件查找

    (一)、命令文件

    [root@linux ~]# chich ls                 //从PATH环境变量
    [root@linux ~]# chereis vim
    
    [root@linux ~]# echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    二、任意文件

    A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
    计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
                  手动更新数据库:updatedb
                  # locate ifcfg-eth0
                  # locate ifcfg-enp0s25

    B. find
    find [options] [path...] [expression] [action]

    (一)、按文件名:

    [root@linux ~]# find /etc -name "ifcfg-eth0"
    [root@linux ~]# find /etc -iname "ifcfg-eth0"            //-i忽略大小写
    [root@linux ~]# find /etc -iname "ifcfg-eth*"

    (二)、按文件大小:

    [root@linux ~]# find /etc -size +5M                        //大于5M
    [root@linux ~]# find /etc -size 5M
    [root@linux ~]# find /etc -size -5M
    [root@linux ~]# find /etc -size +5M -ls                    //-ls找到的处理动作

    (三)、指定查找的目录深度:

    -maxdepth levels
    -mindepth levels
    [root@linux ~]# find / -maxdepth 3 -a  -name "ifcfg-eth0"  //深度3

    (四)、按时间找(atime,mtime,ctime):

    [root@linux ~]# find /etc -mtime +5                     //修改时间超过5天
    [root@linux ~]# find /etc -mtime 5                        //修改时间等于5天
    [root@linux ~]# find /etc -mtime -5                        //修改时间5天以内

    (五)、按文件属主、属组找:

    [root@linux ~]# find /home -user jack                    //属主是jack的文件
    [root@linux ~]# find /home -group hr                    //属组是hr组的文件
    [root@linux ~]# find /home -user jack -group hr
    [root@linux ~]# find /home -user jack -a -group hr
    [root@linux ~]# find /home -user jack -o -group hr
    [root@linux ~]# find /home -nouser
    [root@linux ~]# find /home -nogroup
    [root@linux ~]# find /home -nouser -o -nogroup

    (六)、按文件类型:

    [root@linux ~]# find /dev -type f                           //f普通
    [root@linux ~]# find /dev -type d                         //d目录
    [root@linux ~]# find /dev -type l                           //l链接
    [root@linux ~]# find /dev -type b                         //b块设备
    [root@linux ~]# find /dev -type c                           //c字符设备 
    [root@linux ~]# find /dev -type s                           //s套接字
    [root@linux ~]# find /dev -type p                         //p管道文件

    (七)、按文件权限:

    [root@linux ~]# find . -perm 644 -ls
    [root@linux ~]# find . -perm -644 -ls
    [root@linux ~]# find . -perm -600 -ls
    [root@linux ~]# find . -perm -222 -ls                            //全局可写
    [root@linux ~]# find /usr/bin /usr/sbin -perm -4000 -ls          //包含set uid
    [root@linux ~]# find /usr/bin /usr/sbin -perm -2000 -ls          //包含set gid
    [root@linux ~]# find /usr/bin /usr/sbin -perm -1000 -ls          //包含sticky

    (八)、按正则表达式:

    -regex pattern
    [root@linux ~]# find /etc  -regex  ' .* ifcfg-eth [0-9] '
    .*       任意多个字符
    [0-9]  任意一个数字
    
    [root@linux ~]# find /etc -regex '.*ifcfg-enp0s25'
    /etc/sysconfig/network-scripts/ifcfg-enp0s25
    
    [root@linux ~]# find /etc -regex '.*ifcfg-enp0s[0-9]+'
    /etc/sysconfig/network-scripts/ifcfg-enp0s25

    三、查找之后

      ==找到后处理的动作 ACTIONS: (默认动作-print)==

    -print
    -ls
    -delete
    -exec             后面跟自定义的shell命令
    -ok                后面跟自定义的shell命令
    [root@linux ~]# find /etc -name "ifcfg*"
    [root@linux ~]# find /etc -name "ifcfg*" -print
    [root@linux ~]# find /etc -name "ifcfg*" -ls
    [root@linux ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ;
    [root@linux ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ;
    
    [root@linux ~]# find /etc -name "ifcfg*" -exec rm -rf {} ;
    [root@linux ~]# find /etc -name "ifcfg*" -delete
    
    扩展知识:find结合xargs
    [root@linux ~]# find . -name "yang*.txt" |xargs rm -rf         
    [root@linux ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
  • 相关阅读:
    mysql的备份与恢复(windows、Linux并拷贝至备机)
    eclipse导出可执行jar
    ORCLE中两张表对比更新合入(MERGE INTO)
    js中事件冒泡的问题
    Spring事务传播行为详解
    Java中的锁分类与使用
    用某浏览器全屏延时启动应用
    Springboot整合WebSocket的交互实例(点对点、点对面)
    Windows程序设计------字体不等宽引出的问题及其细节知识
    关于VS2013使用constexpr报错问题
  • 原文地址:https://www.cnblogs.com/sky-k/p/9392722.html
Copyright © 2011-2022 走看看