文件查找命令用法及演示

locatefind

locate
  • 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$'    //运用正则,匹配aaa
/root/aaa
/tmp/mytest/aaa
[root@localhost mytest]# locate -b -r '<aaa>'    //运用正则,匹配aaa
/root/aaa
/tmp/mytest/aaa
[root@localhost mytest]# locate -b -c -r '<aaa$'
2
[root@localhost mytest]#
恢复刚才对updatedb.conf 的修改,不要删除匹配/tmp,因为其下经常会存放的东西比较里面东西比较杂。

find
  • 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

时间图

按照上图,通过设置系统时间,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]#

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 118,387评论 1 239
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 51,599评论 1 200
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 73,683评论 0 167
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 36,160评论 0 127
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 42,971评论 1 205
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 35,672评论 1 124
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 27,638评论 2 206
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 26,785评论 0 119
  • 想象着我的养父在大火中拼命挣扎,窒息,最后皮肤化为焦炭。我心中就已经是抑制不住地欢快,这就叫做以其人之道,还治其人...
    爱写小说的胖达阅读 25,740评论 5 172
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 29,822评论 0 178
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 27,043评论 1 168
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 28,308评论 1 178
  • 白月光回国,霸总把我这个替身辞退。还一脸阴沉的警告我。[不要出现在思思面前, 不然我有一百种方法让你生不如死。]我...
    爱写小说的胖达阅读 22,618评论 0 25
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 25,218评论 2 164
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 29,065评论 3 172
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 24,062评论 0 4
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 24,108评论 0 113
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 30,260评论 2 188
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 30,690评论 2 188

推荐阅读更多精彩内容