locate及find查找命令

在文件系统上查找符合条件的文件:
       实现工具:locate,find

locate:
       依赖于事先构建好的索引库:
       系统自动实现(周期性任务);
       手动更新数据库(updatedb)

       工作特性:
               查找速度;
               模糊查找;
               非实时查找;

yum install -y mlocate
locate [OPTION]... PATTERN...
      命令选项:
              -b:只匹配路径中的基名;
                    path:
                           basename
                           dirname
              -c:统计出共有多少个符合条件的文件
              -r:BRE

注意:索引构建过程(updatedb)需要遍历整个跟文件系统,极消耗资源;

find:
      实时查询工具,通过遍历指定起始路径下文件系统层级结构完成文件查找:

工作特性:
      查找速度略慢;
      精确查找;
      实时查找;

用法:
       find [OPTION] [查找起始路劲] [查找条件] [处理动作]

       查找起始路劲:指定具体搜索目标起始路劲,默认当前目录;
       查找条件:特定的查找标准,可以根据文件名、大小、类型、从属关系、权限等等进行,默认为找出指定路径下的所有文件;
       处理动作:对符合查找条件作出的操作,例如删除等操作,默认为输出至标准输出;

查找条件:
       表达式:选项和测试

测试:结果通常为布尔型(真or假)

文件名查找
       -name;
       -iname;忽略大小写
                通配符?*
       -regex:基于正则表达式模式查找文件,匹配是整个路径,而非其名;

//创建文件    
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth0,IFCFG-ETH0}

//查找/etc目录下宝行ifcfg-eth0名称的文件
[root@localhost ~]# find /etc/ -name 'ifcfg-eth0'
/etc/sysconfig/network-scripts/ifcfg-eth0

//忽略大小写
[root@localhost ~]# find /etc/ -iname 'ifcfg-eth0'
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/IFCFG-ETH0

//查找/etc目录下包含ifcfg-名称的所有文件
[root@localhost ~]# find /etc/ -iname 'ifcfg-*'
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/IFCFG-ETH0


文件大小查找
       -size [+|-] #UNIT
               常用单位:k M G
       #UNIT:(#-1,#)
               5M:(4M,5M)
       -#UNIT:(0,#-1)
               -5M:(0,4M)
       +#UNIT:(,OO)
               +5M:(5,OO)

//查找大于5M
[root@localhost ~]# find /etc/ -size +5M

//创建等于5M的文件
[root@localhost ~]# dd </dev/zero >/etc/test bs=1M count=5

//查找等于5M
[root@localhost ~]# find /etc/ -size 5M

//查找小于5M
[root@localhost ~]# find /etc/ -size -5M
[root@localhost ~]# find /etc/ -size 5M -ls
17586506 5120 -rw-r--r-- 1 root root 5242880 12月 24 16:14 /etc/test
[root@localhost ~]# find /etc/ -size 5M | xargs ls -l
-rw-r--r-- 1 root root 5242880 12月 24 16:14 /etc/test

时间戳查找
       以“天”为单位:
             -atime [+|-]:access time
                     文件访问时间,从CentOS/RHEL6,atime不会随着我们的每次访问而自动更改
                            更新周期:86400(秒)
                            修改文件内容
                            touch
                      #:find /etc -atime 7 之前第七天的文件
                     -#:find /etc -atime -7 七天内的文件
                     +#:find /etc -atime +7 七天之前的文件
              -mtime:
                     最近更改时间
              -ctime
                     最近改动时间

       以“分钟”为单位:
             -amin
             -mmin
             -cmin

//创建测试文件
[root@localhost ~]# for i in {01..31};do date -s 201912$i && touch file-$i;done

//查找7天以前的文件
[root@localhost ~]# find ./ -iname "file-*" -mtime +7

//查找最近7天的文件,不建议使用
[root@localhost ~]# find ./ -iname "file-*" -mtime -7

//查找之前第七天文件
[root@localhost ~]# find ./ -iname "file-*" -mtime 7


文件从属关系查找
      -user:查找属主指定用户的所有文件;
      -group:查找属主指定组的所有文件;
      -uid:查找属主指定UID的所有文件;
      -gid:查找属组指定GID的所有文件;
      -nouser:查找没有属主的文件;
      -nogroup:查找没有属组的文件;

//查找属主是jack
[root@localhost ~]# find /home -user jack

//查找属组是admin
[root@localhost ~]# find /home -group admin

//查找没有属主
[root@localhost ~]# find /home/ -nouser

//查找没有属组
[root@localhost ~]# find /home/ -nogroup


文件类型查找
-type
      f:普通文件
      d:目录文件
      l:符号链接文件
      b:块设备文件
      c:字符设备文件
      p:管道文件
      s:套接字文件

//文件f
[root@localhost ~]# find /dev -type f

//目录d
[root@localhost ~]# find /dev -type d

//链接l
[root@localhost ~]# find /dev -type l

//块设备b
[root@localhost ~]# find /dev -type b

//字符设备c
[root@localhost ~]# find /dev -type c

//套接字s
[root@localhost ~]# find /dev -type s

//管道文件p
[root@localhost ~]# find /run -type p

权限查找
       -perm [/|-] mode
              mode:精确权限匹配
              /mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符号条件即满足;
                      9位权限之间存在“或”关系;
              -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符号条件即满足;
                      9位权限之间存在“与”关系;

//完全匹配644权限
[root@localhost ~]# find . -perm 644 -ls

//拥有者至少有021(-wx),组020(-w-),其他人400(r--)
[root@localhost ~]# find /home -perm -324

//拥有者至少有r,或者组成员至少有r,或者其他人至少有w权限
[root@localhost ~]# find /home -perm /442

//查找全局可写
[root@localhost ~]# find . -perm -222 -ls

//包含uid
[root@localhost ~]# find /usr/sbin/ -perm -4000 -ls
[root@localhost ~]# find /usr/sbin/ -perm -6000 &>/dev/null
//包含sid
[root@localhost ~]# find /usr/sbin/ -perm -2000 -ls

//包含sticky
[root@localhost ~]# find /usr/sbin/ -perm -1000 -ls

组合查找
与:-a(默认)
或:-o
非:-not,!
     !A -a !B=!(A -o B)
     !A -o !B=!(A -a B)

//查找属主是oldboy,属组是admin
[root@localhost ~]# find . -user oldboy -group admin

//查找属主是oldboy,并且属组是admin
[root@localhost ~]# find . -user oldboy -a -group admin

//查找属主是oldboy,或者属组是admin
[root@localhost ~]# find . -user oldboy -o -group admin

//找出/tmp/test目录下属主为非root的所有文件
[root@localhost test]# find . -not -user root -ls
[root@localhost test]# find . ! -user root -ls

//查找没有属组或属组
[root@localhost test]# find . -nouser -o -nogroup

//找出/tmp/test目录下文件名不包含fastab字符串的文件
[root@localhost test]# find . -not -iname "*fastab*" -ls

//找出/tmp目录下属主为非root,而且文件名不包含fstab字符串的文件
[root@localhost test]# find . -not -user root -a -not -iname "*fstab*" -ls
[root@localhost test]# find . -not \( -user root -o -iname "*fstab*" \) -ls


处理动作
当查找到一个文件后,需要对文件进行如何处理
-print:输出至标准输出,默认的动作;
-ls:类似于对查找的文件执行“ls -l”命令,输出文件的详细信息;
-delete:删除查找到的文件;
-fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
-ok COMMAND {} \;:对查找的每个文件执行由COMMAND表示的命令,每次操作都由用户进行确认;
-exec COMMAND {} \;: 对查找的每个文件执行由COMMAND表示的命令,不需确认;

//打印查询到的文件
[root@localhost ~]# find /etc/ -name "ifcfg*" 
[root@localhost ~]# find /etc/ -name "ifcfg*" -ls
[root@localhost ~]# find /etc/ -name "ifcfg*" -print

//拷贝文件
[root@localhost ~]# find /etc/ -name "ifcfg*" -exec cp -rvf {} /tmp \;
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
"/etc/sysconfig/network-scripts/ifcfg-eth0" -> "/tmp/ifcfg-eth0"

//ok会不断提示
[root@localhost ~]# find /etc/ -name "ifcfg*" -ok cp -rvf {} /tmp \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? y
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
< cp ... /etc/sysconfig/network-scripts/ifcfg-eth0 > ? y
"/etc/sysconfig/network-scripts/ifcfg-eth0" -> "/tmp/ifcfg-eth0"

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

//本地文件保留最近7天的备份,备份服务器保留三个月
find /backup -iname "*.bak" -mtime +7 -delete
find /backup -iname "*.bak" -mtime +90 -delete

//给查找到的文件重命名
[root@localhost ~]# find . -name "*txt" -exec mv {} {}.bak \;

注意:find传递查找到的文件路径至后面的命令是,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令,但有些命令不能接受过长的参数,
此时命令执行会失效,另一种方式能规避此问题。
find | xargs COMMAND

猜你喜欢

转载自www.cnblogs.com/xmtxh/p/12093874.html