find 文件查找

2019-03-09  本文已影响0人  晨曦_zdq

1、

[root@ansible-server log]# find /etc/ -size +5M -a -size -10M
/etc/udev/hwdb.bin
[root@ansible-server log]# find /etc/ -size +5M -a -size -10M -ls
50331714 7120 -r--r--r--   1 root     root      7289802 Dec 19 22:36 /etc/udev/hwdb.bin
[root@ansible-server log]# 

注:-a 与关系
-o 或关系

按文件名:

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

指定查找的目录深度:

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

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

[root@xiaochen ~]# find /etc -mtime +5                  //修改时间超过5天
[root@xiaochen ~]# find /etc -mtime 5                       //修改时间等于5天
[root@xiaochen ~]# find /etc -mtime -5                  //修改时间5天以内
注:修改时间5天是按照小时算的,即从此刻往前120h

按文件属主、属组找:

[root@xiaochen ~]# find /home -user jack                    //属主是jack的文件
[root@xiaochen ~]# find /home -group hr                 //属组是hr组的文件
[root@xiaochen ~]# find /home -user jack -group hr   
[root@xiaochen ~]# find /home -user jack -a -group hr    //-a即为and且
[root@xiaochen ~]# find /home -user jack -o -group hr    //-o即为or或
[root@xiaochen ~]# find /home -nouser
[root@xiaochen ~]# find /home -nogroup
[root@xiaochen ~]# find /home -nouser -o -nogroup      #查找没有属主或属组的用户

按文件类型:

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

按文件权限:

[root@xiaochen ~]# find . -perm 644                     //权限为644
[root@xiaochen ~]# find . -perm -644 -ls              //-ls找到的处理动作,权限大于等于644
[root@xiaochen ~]# find . -perm -600 -ls
[root@xiaochen ~]# find /sbin -perm -4000 -ls       //包含set uid
[root@xiaochen ~]# find /sbin -perm -2000 -ls       //包含set gid
[root@xiaochen ~]# find /sbin -perm -1000 -ls       //包含sticky

按正则表达式:

-regex pattern
[root@xiaochen ~]# find /etc  -regex  '.*ifcfg-eth[0-9]'
.*      任意多个字符
[0-9]  任意一个数字

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

-print
-ls
-delete         直接删除找到的文件
-exec           后面跟自定义的shell命令,没有交互,直接执行
-ok               后面跟自定义的shell命令,会询问
[root@xiaochen ~]# find /etc -name "ifcfg*"          //默认就是打印
[root@xiaochen ~]# find /etc -name "ifcfg*" -print
[root@xiaochen ~]# find /etc -name "ifcfg*" -ls
[root@xiaochen ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;     //红色的是语法结构
[root@xiaochen ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;

[root@xiaochen ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@xiaochen ~]# find /etc -name "ifcfg*" -delete
上一篇 下一篇

猜你喜欢

热点阅读