Welcome 微信登录

首页 / 操作系统 / Linux / Linux文件查找工具 locate、find 详解

引言:在学习Linux中的文件查找时,突然联想到平时用的搜索引擎,在生活中我们想获取什么信息,在google等搜索引擎里面敲入就能列出符合我们条件的相关信息。如果我们不满意搜索结果可以进一步精确我们想查找内容的搜索内容,这在Linux的文件查找中称为精确匹配,但是如果我们虽然知道我们想要的内容,但是找不到特别精确的言辞来形容它因此只能输入描述性内容为搜索条件,这种在Linux的文件查找中可以称为模糊匹配。本文的初衷是希望这篇文章可以解决大家在使用Linux的过程中不至于因为查找某个文件找不到而抓狂。                                                                    实验环境CentOS7.2
Linux查找工具Linux中的查找工具有两个:locatefindlocate:非实时查找,根据索引查找find:实时查找,遍历所有文件进行条件匹配
locatelocate属于非实时查找,它根据事先构建的索引数据库,匹配文件信息进而定位符合条件的文件。
其需事先构建的索引数据库在/var/lib/mlocate/mlocate.db,索引的构建过程需要遍历整个根文件系统,极其耗费资源,因此其常常在系统较为空闲时进行(通常设置自动执行),也可以进行手动更新数据库命令为:updatedblocate的工作特性:查找速度快模糊查找非实时查找命令使用格式  locate [option]...PATTEN...    -b:只匹配路径中的基名    -c:统计出共有多少个符合条件的文件    -r:BRE(基本正则表达式)
12345678910111213141516171819202122232425[root@localhost etc]# locate -c file 2540 [root@localhost etc]# locate -b pwd /etc/.pwd.lock /usr/bin/pwd/usr/bin/pwdx/usr/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko /usr/lib64/cracklib_dict.pwd/usr/lib64/python2.7/lib-dynload/spwdmodule.so /usr/sbin/unix_chkpwd/usr/share/cracklib/cracklib-small.pwd/usr/share/cracklib/pw_dict.pwd/usr/share/man/man0p/pwd.h.0p.gz /usr/share/man/man1/pwd.1.gz /usr/share/man/man1/pwdx.1.gz /usr/share/man/man1p/pwd.1p.gz /usr/share/man/man3/lckpwdf.3.gz /usr/share/man/man3/ulckpwdf.3.gz /usr/share/man/man8/unix_chkpwd.8.gz /usr/share/man/overrides/de/man1/pwdx.1 /usr/share/man/overrides/fr/man1/pwdx.1 /usr/share/man/overrides/uk/man1/pwdx.1 [root@localhost test]# locate -r ".*scrip2$"  /test/scrip2[root@localhost test]#
上面演示了locate的三种选项的简单用法
find
find属于实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找。它的工作特性:查找速度略慢、精确查找、实时查找。find [OPTION]... [查找路径] [查找条件] [处理动作]查找路径:指定具体目标路径;默认为当前目录查找条件:???定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件处理动作:对符合条件的文件做操作,默认输出至屏幕………………………………………………………………………………………………………………………查找条件:
表达式:选项和测试(常为布尔型)………………………………………………………………………………………………………………………根据文件名和inode查找:    -name "文件名称":支持使用glob    *, ?, [], [^]    -iname"文件名称":不区分字母大小写    -inumn 按inode号查找    -regex "PATTERN":基于正则表达式模式查找文件,匹配的是整个路径而非其名示例:查找包含有字符串“what”的文件
12345[root@localhost test]# find / -name *what* -ls 72662849    4 drwxr-xr-x   2 root     root         4096 7月 20 00:36 /var/lib/yum/yumdb/v/9879449b2e44ada0445cf495a38c3fd10c942c54-virt-what-1.13-6.el7-x86_64 2536807    4 -rw-r--r--   1 root     root         2836 6月 10  2014 /var/cache/man/cat1/whatis.1.gz 35799386   48 -rwxr-xr-x   1 root     root        46456 6月 10  2014 /usr/bin/whatis...
………………………………………………………………………………………………………………………根据属主、属组查找:    -user USERNAME:查找属主为指定用户(UID)的文件    -group GRPNAME: 查找属组为指定组(GID)的文件    -uidUserID:查找属主为指定的UID号的文件    -gidGroupID:查找属组为指定的GID号的文件    -nouser:查找没有属主的文件    -nogroup:查找没有属组的文件示例:查看/下属于用户fedore的文件,或没有属主的文件
123456789101112131415161718192021[root@localhost test]# find / -user fedore -ls find: ‘/proc/10542/task/10542/fd/6’: 没有那个文件或目录 find: ‘/proc/10542/task/10542/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/10542/fd/6’: 没有那个文件或目录 find: ‘/proc/10542/fdinfo/6’: 没有那个文件或目录 74331703    0 -rw-rw----   1 fedore   mail            0 8月  2 21:49 /var/spool/mail/fedore1477708    4 drwx------   3 fedore   fedore       4096 8月 15 17:11 /home/fedore35996645    0 drwxr-xr-x   4 fedore   fedore         37 7月 19 23:54 /home/fedore/.mozilla 73207822    0 drwxr-xr-x   2 fedore   fedore          6 6月 10  2014 /home/fedore/.mozilla/extensions101849766    0 drwxr-xr-x   2 fedore   fedore          6 6月 10  2014 /home/fedore/.mozilla/plugins1477714    4 -rw-r--r--   1 fedore   fedore         18 11月 20  2015 /home/fedore/.bash_logout 1477721    4 -rw-------   1 fedore   fedore        152 8月 15 17:11 /home/fedore/.history2538688    4 -rw-r--r--   1 fedore   fedore        237 8月 15 16:45 /home/fedore/.bash_profile 2538686    4 -rw-r--r--   1 fedore   fedore        273 8月 15 16:49 /home/fedore/.bashrc 2356350    4 -rw-------   1 fedore   fedore        134 8月 15 17:01 /home/fedore/.Xauthority [root@localhost test]# find / -nouser -ls find: ‘/proc/10540/task/10540/fd/6’: 没有那个文件或目录 find: ‘/proc/10540/task/10540/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/10540/fd/6’: 没有那个文件或目录 find: ‘/proc/10540/fdinfo/6’: 没有那个文件或目录 101603081    0 -rw-r--r--   1 4001     root            0 8月 14 14:47 /tmp/3fstab2
………………………………………………………………………………………………………………………根据文件类型查找:    -type TYPE:    f: 普通文件    d: 目录文件    l: 符号链接文件    s:套接字文件    b: 块设备文件    c: 字符设备文件    p: 管道文件示例:查找/test下的普通文件
12[root@localhost test]# find /test/ -type f -ls 1832646    4 -rwxr-xr-x   1 root     root          399 8月 16 20:44 /test/scrip2
………………………………………………………………………………………………………………………组合条件:    与:-a    或:-o    非:-not, !示例:查找/tmp下属于root且文件类型为普通文件的文件
1234[root@localhost test]# find /tmp/ -user root -a -type f -ls 74330358    4 -rw-r-----   1 root     root         3092 8月 12 21:31 /tmp/vmware-root/vmware-apploader-6127.log 74330359    4 -rw-r-----   1 root     root         3092 8月 12 21:31 /tmp/vmware-root/vmware-apploader-6135.log ...
………………………………………………………………………………………………………………………根据文件大小来查找:    -size [+|-]#UNIT    常用单位:k, M, G    #UNIT: (#-1, #]    如:6k 表示(5k,6k]    -#UNIT:[0,#-1]    如:-6k 表示[0,5k]    +#UNIT:(#,∞)    如:+6k 表示(6k,∞)示例:查找/etc下大于1M的文件
12345[root@localhost test]# find /etc -size +1M -ls    102807508 1304 -rw-------   1 root     root      1333123 8月  2 10:09 /etc/selinux/targeted/contexts/files/file_contexts.bin 36013992 3688 -rw-r--r--   1 root     root      3773309 8月  2 10:09 /etc/selinux/targeted/policy/policy.29 72664102 6852 -r--r--r--   1 root     root      7014922 7月 20 00:38 /etc/udev/hwdb.bin 34828897 1336 -rw-r--r--   1 root     root      1367395 3月  6  2015 /etc/brltty/zh-tw.ctb
………………………………………………………………………………………………………………………根据时间戳以“天”为单位;    -atime[+|-]#,    #: [#,#+1)    +#: [#+1,∞]    -#: [0,#)    -mtime    -ctime以“分钟”为单位:    -amin    -mmin    -cmin示例:查找/etc文件夹下2天内被访问过的文件
12345[root@localhost test]# find /etc -atime -2 -ls 36017835    4 -rw-r--r--   1 root     root           19 7月 20 00:36 /etc/locale.conf 36017836    4 -rw-r--r--   1 root     root           22 7月 20 00:36 /etc/hostname33554565    4 -rw-r--r--   1 root     root         1309 8月 17 08:03 /etc/tpvmlp.conf ...
………………………………………………………………………………………………………………………根据权限查找:    -perm [/|-]MODE    MODE: 精确权限匹配    /MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,     “-perm +mode”从CentOS7后被“-perm /mode”格式替代    -MODE:每一类对象都必须同时拥有指定权限,与关系示例:查找/tmp下至少有一类对象有写权限的文件
123456789[root@localhost ~]# find /tmp -perm /222 -ls 100663425    4 drwxrwxrwt  15 root     root         4096 8月 17 17:36 /tmp34305371    0 drwxrwxrwt   2 root     root            6 7月 20 00:04 /tmp/.Test-unix 69465462    0 drwxrwxrwt   2 root     root           15 8月 17 08:02 /tmp/.X11-unix 74098510    0 srwxrwxrwx   1 root     root            0 8月 17 08:02 /tmp/.X11-unix/X0101599413    0 drwxrwxrwt   2 root     root            6 7月 20 00:04 /tmp/.XIM-unix 760765    0 drwxrwxrwt   2 root     root            6 7月 20 00:04 /tmp/.font-unix 34305372    4 drwxrwxrwt   2 root     root         4096 8月 17 08:03 /tmp/.ICE-unix ...
………………………………………………………………………………………………………………………处理动作:    -print:默认的处理动作,显示至屏幕;    -ls:类似于对查找到的文件执行“ls -l”命令    -delete:删除查找到的文件;    -fls /PATH/TO/SOMEFILE:查找到的所有文件的长格式信息保存至指定文件中    -ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令;对于每个文件执行命令之前,都会交互式要求用户确认    -exec COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;但是有些命令不能接受过长的参数,此时命令执行可能会失败,下面方式可规避此问题    find | xargs COMMAND示例:1)查找/test下名为scrip2的文件,并用cat查看其内容,使用两种方式;2)将搜索到的scrip2文件的长格式信息保存在/test/scrip1中
123456789101112[root@localhost test]# find /test -name scrip2 -exec cat {} ; #!/bin/bash # [ $# -lt 1 ] && echo "please give one IP address" && exit 1 [ $# -gt 1 ] && echo "this script just match one IP address" && exit 2 [ $# -eq 1 ] && [[ $1 =~ (([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]).){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]) ]] && ping -c 1 $1 | sed -n "2p"|grep -o "^64" >> /dev/null && echo "该IP地址可访问" || echo "该IP地址不可访问" [root@localhost test]# find /test -name scrip2 | xargs cat #!/bin/bash # [ $# -lt 1 ] && echo "please give one IP address" && exit 1 [ $# -gt 1 ] && echo "this script just match one IP address" && exit 2 [ $# -eq 1 ] && [[ $1 =~ (([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]).){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]) ]] && ping -c 1 $1 | sed -n "2p"|grep -o "^64" >> /dev/null && echo "该IP地址可访问" || echo "该IP地址不可访问"
2)将搜索到的scrip2文件的长格式信息保存在/test/scrip1中
123[root@localhost test]# find /test -name scrip2 -fls /test/scrip1 [root@localhost test]# cat scrip1 1832646    4 -rwxr-xr-x   1 root     root          399 8月 16 20:44 /test/scrip2

locate与find的不同locate由于非实时因此会查找到最近删除的文件,而find则不会。上述的不同导致查找的结果有所不同。示例:在/test/文件下有两个文件scrip1、scrip2,删除scrip1,然后在搜scrip1
12345678910[root@localhost test]# stat /var/lib/mlocate/mlocate.db     文件:"/var/lib/mlocate/mlocate.db"  大小:2771084         块:5416       IO 块:4096   普通文件 设备:fd00h/64768d      Inode:35764072    硬链接:1 权限:(0640/-rw-r-----)  Uid:(    0/    root)   Gid:(   21/ slocate) 环境:system_u:object_r:locate_var_lib_t:s0 最近访问:2016-08-17 16:03:17.734189941 +0800 最近更改:2016-08-17 09:10:04.982775027 +0800 最近改动:2016-08-17 09:10:04.983775043 +0800 创建时间:-
先查看下该索引数据库的最新改动时间,该时间是上午的,所以现在我们删除scrip1文件后locate是可以搜到的。
123456789[root@localhost test]# ll 总用量 8 -rw-r--r--. 1 root root  82 8月  17 18:03 scrip1 -rwxr-xr-x. 1 root root 399 8月  16 20:44 scrip2 [root@localhost test]# rm scrip1 rm:是否删除普通文件 "scrip1"?y [root@localhost test]# ll 总用量 4 -rwxr-xr-x. 1 root root 399 8月  16 20:44 scrip2
删除scrip1,然后搜索它
1234[root@localhost test]# locate scrip1  /test/scrip1[root@localhost test]# find /test/ -name scrip1 [root@localhost test]#
比较结束。通过结果,如果我们要求搜索结果靠谱最好还是使用find。 
 为了对find有更深的了解下面举了关于find的8个例子 1、查找/var目录下属主为root,且属组为mail的所有文件
123[root@localhost ~]# find /var/ -user root -group mail -ls 67151194    4 drwxrwxr-x   2 root     mail         4096 8月 15 16:37 /var/spool/mail72699698    4 -rw-------   1 root     mail         2220 8月  2 19:41 /var/spool/mail/root
2、查找/var目录下不属于root、fedore、mail的所有文件
12345[root@localhost ~]# find /var/ -not -user root -a -not -user fedore -a -not -user mail -ls 69508348    0 drwxr-xr-x   2 unbound  unbound        21 7月 20 00:05 /var/lib/unbound69508349    4 -rw-r--r--   1 unbound  unbound       409 11月 20  2015 /var/lib/unbound/root.key 780655    0 drwx------   2 tss      tss             6 11月 21  2015 /var/lib/tpm...
3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
1234[root@localhost ~]# find /var/ -mtime -7 -a -not -user root -a -not -user postfix -ls 69508348    0 drwxr-xr-x   2 unbound  unbound        21 7月 20 00:05 /var/lib/unbound780655    0 drwx------   2 tss      tss             6 11月 21  2015 /var/lib/tpm101711459    0 drwxr-xr-x   3 colord   colord         50 7月 19 16:46 /var/lib/colord
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
123456[root@localhost ~]# find / -nouser -a -atime -7 -ls find: ‘/proc/2254/task/2254/fd/6’: 没有那个文件或目录 find: ‘/proc/2254/task/2254/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/2254/fd/6’: 没有那个文件或目录 find: ‘/proc/2254/fdinfo/6’: 没有那个文件或目录 101603081    0 -rw-r--r--   1 4001     root            0 8月 14 14:47 /tmp/3fstab2
5、查找/etc目录下大于1M且类型为普通文件的所有文件
12345[root@localhost ~]# find /etc -size +1M -a -type f -ls 102807508 1304 -rw-------   1 root     root      1333123 8月  2 10:09 /etc/selinux/targeted/contexts/files/file_contexts.bin 36013992 3688 -rw-r--r--   1 root     root      3773309 8月  2 10:09 /etc/selinux/targeted/policy/policy.29 72664102 6852 -r--r--r--   1 root     root      7014922 7月 20 00:38 /etc/udev/hwdb.bin 34828897 1336 -rw-r--r--   1 root     root      1367395 3月  6  2015 /etc/brltty/zh-tw.ctb
6、查找/etc目录下所有用户都没有写权限的文件
12345[root@localhost ~]# find /etc/ -not -perm /222 -ls 190164  196 -r--r--r--   1 root     root       198453 7月 19 23:59 /etc/pki/ca-trust/extracted/java/cacerts33828600  352 -r--r--r--   1 root     root       359773 7月 19 23:59 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt 67873473  264 -r--r--r--   1 root     root       266702 7月 19 23:59 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem ...
7、查找/etc目录下至少有一类用户没有执行权限的文件
12345[root@localhost ~]# find /etc/ -not -perm -222 -ls 36017835    4 -rw-r--r--   1 root     root           19 7月 20 00:36 /etc/locale.conf 36017836    4 -rw-r--r--   1 root     root           22 7月 20 00:36 /etc/hostname35930065    4 -rw-r--r--   1 root     root          163 7月 19 23:54 /etc/.updated ...
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
1[root@localhost ~]# find /etc/init.d/ -perm -113
Linux find 命令用法总结  http://www.linuxidc.com/Linux/2015-04/116854.htmLinux下的文件查找命令——find  http://www.linuxidc.com/Linux/2016-05/131826.htmLinux下查找文件find命令  http://www.linuxidc.com/Linux/2014-10/108575.htmLinux下find命令详解 http://www.linuxidc.com/Linux/2011-08/40669.htm文本查找利器find的使用 http://www.linuxidc.com/Linux/2014-03/97559.htm功能强大的find命令 http://www.linuxidc.com/Linux/2014-01/95236.htmLinux系统find命令详解 http://www.linuxidc.com/Linux/2014-06/103232.htm本文永久更新链接地址