|
引言:
在学习Linux中的文件查找时,突然联想到平时用的搜索引擎,在生活中我们想获取什么信息,在google等搜索引擎里面敲入就能列出符合我们条件的相关信息。如果我们不满意搜索结果可以进一步精确我们想查找内容的搜索内容,这在Linux的文件查找中称为精确匹配,但是如果我们虽然知道我们想要的内容,但是找不到特别精确的言辞来形容它因此只能输入描述性内容为搜索条件,这种在Linux的文件查找中可以称为模糊匹配。本文的初衷是希望这篇文章可以解决大家在使用Linux的过程中不至于因为查找某个文件找不到而抓狂。
Linux查找工具
Linux中的查找工具有两个:locate、find
locate:非实时查找,根据索引查找
find:实时查找,遍历所有文件进行条件匹配
locate
locate属于非实时查找,它根据事先构建的索引数据库,匹配文件信息进而定位符合条件的文件。
其需事先构建的索引数据库在/var/lib/mlocate/mlocate.db,索引的构建过程需要遍历整个根文件系统,极其耗费资源,因此其常常在系统较为空闲时进行(通常设置自动执行),也可以进行手动更新数据库命令为:updatedb
locate的工作特性:查找速度快、模糊查找、非实时查找
命令使用格式
locate [option]...PATTEN...
-b:只匹配路径中的基名
-c:统计出共有多少个符合条件的文件
-r:BRE(基本正则表达式)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| [iyunv@localhost etc]# locate -c file
2540
[iyunv@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
[iyunv@localhost test]# locate -r '.*scrip2$'
/test/scrip2
[iyunv@localhost test]#
|
上面演示了locate的三种选项的简单用法
find
find属于实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找。
它的工作特性:查找速度略慢、精确查找、实时查找。
find [OPTION]... [查找路径] [查找条件] [处理动作]
查找路径:指定具体目标路径;默认为当前目录
查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件
处理动作:对符合条件的文件做操作,默认输出至屏幕
………………………………………………………………………………………………………………………
查找条件:
表达式:选项和测试(常为布尔型)
………………………………………………………………………………………………………………………
根据文件名和inode查找:
-name "文件名称":支持使用glob
*, ?, [], [^]
-iname"文件名称":不区分字母大小写
-inumn 按inode号查找
-regex "PATTERN":基于正则表达式模式查找文件,匹配的是整个路径而非其名
示例:查找包含有字符串“what”的文件
1
2
3
4
5
| [iyunv@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的文件,或没有属主的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [iyunv@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/fedore
1477708 4 drwx------ 3 fedore fedore 4096 8月 15 17:11 /home/fedore
35996645 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/extensions
101849766 0 drwxr-xr-x 2 fedore fedore 6 6月 10 2014 /home/fedore/.mozilla/plugins
1477714 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/.history
2538688 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
[iyunv@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下的普通文件
1
2
| [iyunv@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且文件类型为普通文件的文件
1
2
3
4
| [iyunv@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的文件
1
2
3
4
5
| [iyunv@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天内被访问过的文件
1
2
3
4
5
| [iyunv@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/hostname
33554565 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下至少有一类对象有写权限的文件
1
2
3
4
5
6
7
8
9
| [iyunv@localhost ~]# find /tmp -perm /222 -ls
100663425 4 drwxrwxrwt 15 root root 4096 8月 17 17:36 /tmp
34305371 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/X0
101599413 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中
1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@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地址不可访问"
[iyunv@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中
1
2
3
| [iyunv@localhost test]# find /test -name scrip2 -fls /test/scrip1
[iyunv@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
1
2
3
4
5
6
7
8
9
10
| [iyunv@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是可以搜到的。
1
2
3
4
5
6
7
8
9
| [iyunv@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
[iyunv@localhost test]# rm scrip1
rm:是否删除普通文件 "scrip1"?y
[iyunv@localhost test]# ll
总用量 4
-rwxr-xr-x. 1 root root 399 8月 16 20:44 scrip2
|
删除scrip1,然后搜索它
1
2
3
4
| [iyunv@localhost test]# locate scrip1
/test/scrip1
[iyunv@localhost test]# find /test/ -name scrip1
[iyunv@localhost test]#
|
比较结束。通过结果,如果我们要求搜索结果靠谱最好还是使用find。
为了对find有更深的了解下面举了关于find的8个例子
1、查找/var目录下属主为root,且属组为mail的所有文件
1
2
3
| [iyunv@localhost ~]# find /var/ -user root -group mail -ls
67151194 4 drwxrwxr-x 2 root mail 4096 8月 15 16:37 /var/spool/mail
72699698 4 -rw------- 1 root mail 2220 8月 2 19:41 /var/spool/mail/root
|
2、查找/var目录下不属于root、fedore、mail的所有文件
1
2
3
4
5
| [iyunv@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/unbound
69508349 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的文件
1
2
3
4
| [iyunv@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/unbound
780655 0 drwx------ 2 tss tss 6 11月 21 2015 /var/lib/tpm
101711459 0 drwxr-xr-x 3 colord colord 50 7月 19 16:46 /var/lib/colord
|
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
1
2
3
4
5
6
| [iyunv@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且类型为普通文件的所有文件
1
2
3
4
5
| [iyunv@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目录下所有用户都没有写权限的文件
1
2
3
4
5
| [iyunv@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/cacerts
33828600 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目录下至少有一类用户没有执行权限的文件
1
2
3
4
5
| [iyunv@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/hostname
35930065 4 -rw-r--r-- 1 root root 163 7月 19 23:54 /etc/.updated
...
|
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
1
| [iyunv@localhost ~]# find /etc/init.d/ -perm -113
|
|
|