美文网首页linux系统
linux常用命令:find精确查找

linux常用命令:find精确查找

作者: 一个小运维 | 来源:发表于2021-06-21 09:55 被阅读0次
    格式:
    find    [目录]   [条件1] 
    
    • 常用条件表示:
    -type  类型(f、d、l)
    -name  "文档名称"
    -size  +|-文件大小(k、M、G)
    -user  用户名
    -mtime  修改时间
    

    -type 类型(f文本文件、d目录、l快捷方式)

    [root@A /]# find  /boot  -type  d
    [root@A /]# find  /opt  -type  d
    [root@A /]# find  /etc  -type  l   
    [root@A /]# find  /boot  -type  f
    

    -name "文档名称"

    [root@A /]# find  /etc/  -name  "passwd"
    [root@A /]# find  /etc/  -name  "*.conf"
    
    [root@A /]# find  /etc/  -name  "*.conf"  |  wc  -l
    [root@A /]# find  /etc/  -name  "*.conf"  |  cat  -n
    
    [root@A /]# find /mnt/  -name  "nsd*"
    [root@A /]# find /mnt/  -name  "nsd*"  -type d
    [root@A /]# find /mnt/  -name  "nsd*"  -type f
    

    -size +或- 文件大小(k、M、G)

    [root@A /]# find  /boot/  -size  +300k
    [root@A /]# find  /boot/  -size  +10M
    

    -user 用户名 (按照数据的所有者)

    [root@A /]# useradd  natasha  #创建用户
    [root@A /]# find  /home/  -user  natasha
    [root@A /]# find  /   -user  natasha
    

    -mtime 修改时间 (所有的时间都是过去时间)

    -mtime  +90  #90天之前的数据
    -mtime  -90   #最近90天之内的数据
    
    [root@A /]# find  /root  -mtime  +90
    [root@A /]# find  /root  -mtime  -10
    
    find高级使用
    • 处理find找到的数据,每查找的一个就传递一次
    –find  [范围]  [条件]  -exec  处理命令  {}   \;
    -exec额外操作的开始
    {} 永远表示前面find查找的结果
    \;  额外操作的结束
    
    [root@A /]# find /boot/ -size +10M
    [root@A /]# find /boot/ -size +10M -exec  cp  {}  /mnt  \;
    [root@A /]# ls /mnt/
    
    • 查找并处理文件
      利用find查找所有用户 student 拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中
    [root@A /]# useradd  student      
    [root@A /]# mkdir  /root/findfiles
    
    [root@A /]# find  /  -user student  -type  f
    [root@A /]# find  /  -user student  -type  f  -exec cp  {}  /root/findfiles  \;
    [root@A /]# ls  -A  /root/findfiles/ 
    

    相关文章

      网友评论

        本文标题:linux常用命令:find精确查找

        本文链接:https://www.haomeiwen.com/subject/pcoeyltx.html