文件查找locate、find

文件查找

grep:  文件内容过滤
find:   文件查找,针对文件名
一、命令文件 
# which ls                      //从PATH环境变量 (echo $PATH)
# whereis vim
二、任意文件
A.  locate   (查询的数据库: /var/lib/mlocate/mlocate.db)  
   计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
   手动更新数据库:updatedb

                  # locate ifcfg-eth0

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

按文件名:
[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0"    //-i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"


按文件大小:
[root@localhost ~]# find /etc -size +5M    //大于5M
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M
[root@localhost ~]# find /etc -size +5M -ls    //-ls找到的处理动作


指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@localhost~]# find / -maxdepth 3 -a  -name "ifcfg-eth0"


按时间找(atime,mtime,ctime):
[root@localhost ~]# find /etc -mtime +5    //修改时间超过5天
[root@localhost ~]# find /etc -mtime 5 //修改时间等于5天
[root@localhost ~]# find /etc -mtime -5    //修改时间5天以内

按文件属主、属组找:
[root@localhost ~]# find /home -user jack    //属主是jack的文件
[root@localhost ~]# find /home -group hr    //属组是hr组的文件
[root@localhost ~]# find /home -user jack -group hr
[root@localhost ~]# find /home -user jack -a -group hr
[root@localhost ~]# find /home -user jack -o -group hr

[root@localhost ~]# find /home -nouser
[root@localhost ~]# find /home -nogroup
[root@localhost ~]# find /home -nouser -o -nogroup 


按文件类型:
[root@localhost ~]# find /dev -type f   //f普通
[root@localhost ~]# find /dev -type d   //d目录
[root@localhost ~]# find /dev -type l   //l链接
[root@localhost ~]# find /dev -type b   //b块设备
[root@localhost ~]# find /dev -type c   //c字符设备
[root@localhost ~]# find /dev -type s   //s套接字
[root@localhost ~]# find /dev -type p   //p管道文件
  
按文件权限:
[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find . -perm -222 -ls                 //全局可写
[root@localhost ~]# find /sbin -perm -4000 -ls  //包含set uid
[root@localhost ~]# find /sbin -perm -2000 -ls  //包含set gid
[root@localhost ~]# find /sbin -perm -1000 -ls  //包含sticky

按正则表达式:
-regex pattern
[root@localhost ~]# find /etc  -regex  '.*ifcfg-eth[0-9]'
.*      任意多个字符
[0-9]  任意一个数字


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

-print
-ls
-delete
-exec            后面跟自定义的shell命令
-ok               后面跟自定义的shell命令
[root@localhost ~]# find /etc -name "ifcfg*"
[root@localhost ~]# find /etc -name "ifcfg*" -print
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;


[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -delete

注:find结合xargs
[root@localhost ~]# find . -name "yang*.txt" |xargs rm -rf
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值