文件查找命令用法及演示

locatefind

locate
  • 文件查找命令用法及演示_第1张图片
    locate
  • 相关文件
    /var/lib/mlocate/mlocate.db 默认的数据库(此为二进制文件)
    /etc/updatedb.conf updatedb更新数据库文件的配置文件,可以通过man 5 updatedb.conf手册查看文件介绍
  • 命令演示

[root@localhost mytest]# touch aaa   //在/tmp/mytest目录下创建文件aaa
[root@localhost mytest]# touch ~/aaa   //在root的家目录下创建文件aaa
[root@localhost mytest]# ll aaa
-rw-r--r--. 1 root root 0 9月 8 01:33 aaa
[root@localhost mytest]# ll ~ | head -2
总用量 16
-rw-r--r--. 1 root root 0 9月 8 01:33 aaa
[root@localhost mytest]# locate aaa   //此时是找不到的
/usr/share/man/man3/ldns_rdf2buffer_str_aaaa.3.gz
/var/cache/fontconfig/614d1caaa4d7914789410f6367de37ca-le64.cache
/var/cache/fontconfig/b79f3aaa7d385a141ab53ec885cc22a8-le64.cache
[root@localhost mytest]# updatedb    //更新数据库文件
[root@localhost mytest]# locate aaa    // /tmp/mytest目录下的aaa找不到
/root/aaa
/usr/share/man/man3/ldns_rdf2buffer_str_aaaa.3.gz
/var/cache/fontconfig/614d1caaa4d7914789410f6367de37ca-le64.cache
/var/cache/fontconfig/b79f3aaa7d385a141ab53ec885cc22a8-le64.cache
[root@localhost mytest]# cat /etc/updatedb.conf    //查看配置文件
PRUNE_BIND_MOUNTS = "yes"
sysfs tmpfs ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
[root@localhost mytest]# cat /etc/updatedb.conf | grep -E '/tmp'    //加粗为正则匹配的内容
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
[root@localhost mytest]# locate -r '^/tmp$'    //目录本身是可以找到的,其下的内容是找不到的
/tmp
[root@localhost mytest]# vim /etc/updatedb.conf    //vim临时修改配置文件
[root@localhost mytest]# cat /etc/updatedb.conf | grep -E '/tmp'    //已去掉/tmp目录
PRUNEPATHS = "/afs /media /mnt /net /sfs /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
[root@localhost mytest]# updatedb    //更新数据库文件
[root@localhost mytest]# locate aaa
/root/aaa
/root/dyaaa
/root/dyaaa/test
/tmp/mytest/aaa
/usr/share/man/man3/ldns_rdf2buffer_str_aaaa.3.gz
/var/cache/fontconfig/614d1caaa4d7914789410f6367de37ca-le64.cache
/var/cache/fontconfig/b79f3aaa7d385a141ab53ec885cc22a8-le64.cache
[root@localhost mytest]# locate -c aaa
7
[root@localhost mytest]# locate -b aaa    //只匹配基名,和上个比较少了一个
/root/aaa
/root/dyaaa
/tmp/mytest/aaa
/usr/share/man/man3/ldns_rdf2buffer_str_aaaa.3.gz
/var/cache/fontconfig/614d1caaa4d7914789410f6367de37ca-le64.cache
/var/cache/fontconfig/b79f3aaa7d385a141ab53ec885cc22a8-le64.cache
[root@localhost mytest]# locate -b -c aaa
6
[root@localhost mytest]# locate -b -r 'aaa$'    //运用正则,匹配aaa
/root/aaa
/root/dyaaa
/tmp/mytest/aaa
[root@localhost mytest]# locate -b -r '    //运用正则,匹配aaa
/root/aaa
/tmp/mytest/aaa
[root@localhost mytest]# locate -b -r ''    //运用正则,匹配aaa
/root/aaa
/tmp/mytest/aaa
[root@localhost mytest]# locate -b -c -r '
2
[root@localhost mytest]#
恢复刚才对updatedb.conf 的修改,不要删除匹配/tmp,因为其下经常会存放的东西比较里面东西比较杂。

find
  • 文件查找命令用法及演示_第2张图片
    find
  • 命令演示

[root@localhost mytest]# find -name aaa
./aaa
[root@localhost mytest]# find -name Aaa
[root@localhost mytest]# find -iname Aaa    //忽略大小写
./aaa
[root@localhost xiangjis]# find -name "du*"    //支持globbing风格的通配,需用引号
./du
./du1
./du2
./du4
[root@localhost xiangjis]# find -iname "du*"
./du
./du1
./du2
./du4
[root@localhost xiangjis]# find -name "du[0-9]"
./du1
./du2
./du4
[root@localhost xiangjis]# find -name "du?"
./du1
./du2
./du4
[root@localhost mytest]# find ./etc -name passwd
./etc/passwd
./etc/pam.d/passwd
[root@localhost mytest]# find ./etc -regex passwd
[root@localhost mytest]# find ./etc -regex .*passwd    //运用正则,全路径的匹配,只写文件名是不可以得
./etc/passwd
./etc/pam.d/passwd
./etc/security/opasswd
[root@localhost mytest]# find ./etc -regex .*passWd
[root@localhost mytest]# find ./etc -iregex .*passWd    //忽略大小写
./etc/passwd
./etc/pam.d/passwd
./etc/security/opasswd
[root@localhost mytest]# ll
总用量 40
-rw-rw-r--. 1 hadoop hadoop 0 9月 8 05:00 du
-rw-rw-r--. 1 fedora fedora 0 9月 8 05:02 du1
-rw-r--r--. 1 fedora centos 0 9月 8 05:04 du2

-rw-r--r--. 1 root root 38476 9月 1 13:44 vim-586.test
drwxrwxr-x. 2 hadoop hadoop 21 9月 8 05:00 xiangjis
drwxrwxr-x. 2 fedora fedora 22 9月 8 05:02 xiangjis1
drwxr-xr-x. 2 fedora centos 22 9月 8 05:05 xiangjis2
[root@localhost mytest]# find -user fedora
./du1
./xiangjis1
./xiangjis1/du1.test
./du2
./xiangjis2
./xiangjis2/du2.test
[root@localhost mytest]# find -group centos
./du2
./xiangjis2
./xiangjis2/du2.test
[root@localhost mytest]# grep -E 'centos\>' /etc/group    //查看group配置文件中的组centos
centos:x:1006:centos2
[root@localhost mytest]# ll xiangjis2
总用量 0
-rw-r--r--. 1 fedora centos 0 9月 8 05:05 du2.test
[root@localhost mytest]# groupdel centos    //删除组centos
[root@localhost mytest]# ll xiangjis2    //删除组名后,文件所属组处只有组ID号
总用量 0
-rw-r--r--. 1 fedora 1006 0 9月 8 05:05 du2.test
[root@localhost mytest]# find -nogroup
./du2
./xiangjis2
./xiangjis2/du2.test
[root@localhost mytest]# find -gid 1006
./du2
./xiangjis2
./xiangjis2/du2.test
[root@localhost mytest]# find /home -nouser
/home/mandriva
/home/mandriva/.bash_logout
..........
[root@localhost mytest]# grep -E '(mandriva)|(fedora)' /etc/passwd    //passwd文件中没有用户mandriva
fedora:x:1004:1005::/home/fedora:/bin/bash
[root@localhost mytest]# ll du2
-rw-r--r--. 1 fedora 1006 5 9月 8 05:28 du2
[root@localhost mytest]# ll -d xiangjis2
drwxr-xr-x. 2 fedora 1006 22 9月 8 05:05 xiangjis2
[root@localhost mytest]# ll xiangjis2
总用量 0
-rw-r--r--. 1 fedora 1006 0 9月 8 05:05 du2.test
[root@localhost mytest]# find -nogroup -type f
./du2
./xiangjis2/du2.test
[root@localhost mytest]# find -nogroup -type d
./xiangjis2
[root@localhost mytest]# find -nouser -o -nogroup
./du2
./xiangjis2
./xiangjis2/du2.test
[root@localhost mytest]# find -user fedora -a -nogroup
./du2
./xiangjis2
./xiangjis2/du2.tes

文件查找命令用法及演示_第3张图片
时间图

按照上图,通过设置系统时间,touch文件,修改文件的时间戳

[root@localhost xiangjis]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 7 00:00 du
-rw-r--r--. 1 root root 0 9月 6 00:00 du1
-rw-r--r--. 1 root root 0 9月 5 03:00 du2
-rw-rw-r--. 1 hadoop hadoop 0 9月 8 2017 du.test
[root@localhost xiangjis]# date -s 170907    //设定当前的时间
2017年 09月 07日 星期四 00:00:00 CST
[root@localhost xiangjis]# touch du    //通过touch命令修改文件的时间戳
[root@localhost xiangjis]# date -s 170906
2017年 09月 06日 星期三 00:00:00 CST
[root@localhost xiangjis]# touch du1
[root@localhost xiangjis]# date -s "170905 3"
2017年 09月 05日 星期二 03:00:00 CST
[root@localhost xiangjis]# touch du2
[root@localhost xiangjis]# stat du    //主要关注ctime时间
文件:"du"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34471010 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-09-07 00:00:07.046960844 +0800
最近更改:2017-09-07 00:00:07.046960844 +0800
最近改动:2017-09-07 00:00:07.046960844 +0800
创建时间:-
[root@localhost xiangjis]# stat du1
文件:"du1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34471011 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-09-06 00:00:03.169982385 +0800
最近更改:2017-09-06 00:00:03.169982385 +0800
最近改动:2017-09-06 00:00:03.169982385 +0800
创建时间:-
[root@localhost xiangjis]# stat du2
文件:"du2"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34471012 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-09-05 03:00:06.978961218 +0800
最近更改:2017-09-05 03:00:06.978961218 +0800
最近改动:2017-09-05 03:00:06.978961218 +0800
创建时间:-
[root@localhost xiangjis]# stat du.test
文件:"du.test"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34471047 硬链接:1
权限:(0664/-rw-rw-r--) Uid:( 2003/ hadoop) Gid:( 2019/ hadoop)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-09-07 21:37:33.732205785 +0800
最近更改:2017-09-08 05:00:56.076995716 +0800
最近改动:2017-09-08 05:00:56.076995716 +0800
创建时间:-
[root@localhost xiangjis]# date -s "170907 15" //修改系统时间为当前时间
2017年 09月 07日 星期四 15:00:00 CST
[root@localhost xiangjis]# find -ctime 1
./du1
[root@localhost xiangjis]# find -ctime +1
./du2
[root@localhost xiangjis]# find -ctime -1    //多出来个du.test;个人理解应该为其ctime时间为比当前时间超前的原因吧
.
./du.test
./du
[root@localhost xiangjis]# date -s '170905 16'    
2017年 09月 05日 星期二 16:00:00 CST
[root@localhost xiangjis]# touch du.test    
[root@localhost xiangjis]# stat du.test    //修改其ctime时间和du1在一个区间内
文件:"du.test"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:34471047 硬链接:1
权限:(0664/-rw-rw-r--) Uid:( 2003/ hadoop) Gid:( 2019/ hadoop)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2017-09-05 16:00:07.272959584 +0800
最近更改:2017-09-05 16:00:07.272959584 +0800
最近改动:2017-09-05 16:00:07.272959584 +0800
创建时间:-
[root@localhost xiangjis]# date -s '170907 15'
2017年 09月 07日 星期四 15:00:00 CST
[root@localhost xiangjis]# find -ctime -1
.
./du
[root@localhost xiangjis]# find -ctime 1
./du.test
./du1
[root@localhost xiangjis]# find -ctime +1
./du2
[root@localhost xiangjis]#
[root@localhost xiangjis]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 7 00:00 du
-rw-r--r--. 1 root root 0 9月 6 00:00 du1
-rw-r--r--. 1 root root 0 9月 5 03:00 du2
-rw-rw-r--. 1 hadoop hadoop 0 9月 5 16:00 du.test
[root@localhost xiangjis]# find -perm 644    //精确查找3类用户的权限位
./du
./du1
./du2
[root@localhost xiangjis]# touch du3    
[root@localhost xiangjis]# ll du3
-rw-r--r--. 1 root root 0 9月 7 15:12 du3    
[root@localhost xiangjis]# chmod 004 du3    //设置du3文件权限为o=r
[root@localhost xiangjis]# ll du3
-------r--. 1 root root 0 9月 7 15:12 du3
[root@localhost xiangjis]# find -perm 4    //查找其他用户权限为只读的文件
./du3
[root@localhost xiangjis]# chmod 064 du3
[root@localhost xiangjis]# ll du3
----rw-r--. 1 root root 0 9月 7 15:12 du3
[root@localhost xiangjis]# find -perm 064    //查找其他用户权限为只读,同时组权限为读写的文件
./du3
[root@localhost xiangjis]# find -perm 64    //同上
./du3
[root@localhost xiangjis]# ll du3
---------x. 1 root root 0 9月 7 15:12 du3
[root@localhost xiangjis]# find -perm /1    //匹配权限位有执行权限的文件
.
./du3
[root@localhost xiangjis]# find -perm /001    //同上
.
./du3
[root@localhost xiangjis]# find -perm -4    //匹配三类用户的权限位都含有读权限的文件
.
./du.test
./du
./du1
./du2
[root@localhost xiangjis]# chmod a+w du3
[root@localhost xiangjis]# ll du3
--w--w--wx. 1 root root 0 9月 7 15:12 du3
[root@localhost xiangjis]# find -perm -2    //匹配三类用户的权限位都含有写权限的文件
./du3
[root@localhost xiangjis]# find -perm /1 -ls    //匹配9位权限位中含有执行权限的文件和目录,并ls出来
34471046 0 drwxrwxr-x 2 hadoop hadoop 64 9月 7 15:12 .
34471008 0 --w--w--wx 1 root root 0 9月 7 15:12 ./du3
[root@localhost xiangjis]# find -perm /1 -type f -ls    //匹配9位权限位中含有执行权限的文件,并ls出来
34471008 0 --w--w--wx 1 root root 0 9月 7 15:12 ./du3
[root@localhost xiangjis]# find -perm /1 -type f -delete    //匹配9位权限位中含有执行权限的文件,并删除处理
[root@localhost xiangjis]# ll du3
ls: 无法访问du3: 没有那个文件或目录
[root@localhost xiangjis]# find -user root -fls du4    //匹配属组为root的文件列表,并保存到文件du4中
[root@localhost xiangjis]# cat du4
34471010 0 -rw-r--r-- 1 root root 0 9月 7 00:00 ./du
34471011 0 -rw-r--r-- 1 root root 0 9月 6 00:00 ./du1
34471012 0 -rw-r--r-- 1 root root 0 9月 5 03:00 ./du2
34471008 0 -rw-r--r-- 1 root root 0 9月 7 15:24 ./du4
[root@localhost xiangjis]#
[root@localhost mytest]# find -nogroup -ls    //查找么有属组的文件
34471048 4 -rw-r--r-- 1 fedora 1006 5 9月 8 05:28 ./du2
101228615 0 drwxr-xr-x 2 fedora 1006 22 9月 8 05:05 ./xiangjis2
101228616 0 -rw-r--r-- 1 fedora 1006 0 9月 8 05:05 ./xiangjis2/du2.test
[root@localhost mytest]# find -nogroup -ok chgrp fedora {} ;    //找到没有属组的文件,并配置其属组为fedora。注意命令中的英文分号不能漏掉
< chgrp ... ./du2 > ? yes
< chgrp ... ./xiangjis2 > ? yes
< chgrp ... ./xiangjis2/du2.test > ? yes
[root@localhost mytest]# find -group fedora -ls
34471048 4 -rw-r--r-- 1 fedora fedora 5 9月 8 05:28 ./du2
101228615 0 drwxr-xr-x 2 fedora fedora 22 9月 8 05:05 ./xiangjis2
101228616 0 -rw-r--r-- 1 fedora fedora 0 9月 8 05:05 ./xiangjis2/du2.test
[root@localhost mytest]# find -user fedora -type f    //查找属主为fedora的文件
./du1
./xiangjis1/du1.test
./du2
./xiangjis2/du2.test
[root@localhost mytest]# find -user fedora -type f -ok rm {} ;    //找到并进行删除操作,每此删除操作都进行询问
< rm ... ./du1 > ? yes
< rm ... ./xiangjis1/du1.test > ? yes
< rm ... ./du2 > ? yes
< rm ... ./xiangjis2/du2.test > ? yes
[root@localhost mytest]# find -user fedora -type f
[root@localhost mytest]# find -user fedora -type d    //查找属主为fedora的目录
./xiangjis1
./xiangjis2
[root@localhost mytest]# find -user fedora -type f
./du4
./du3
[root@localhost mytest]# find -user fedora -exec rm -v {} ;    //找到并进行删除操作,每此删除操作不进行询问
已删除"./du4"
已删除"./du3"
[root@localhost mytest]# find -user fedora -type f
[root@localhost mytest]#

你可能感兴趣的:(文件查找命令用法及演示)