Linux上的文件搜索命令实例详解

locate

基础了解

在centos7上默认没有locate命令,需要先手动安装。安装步骤:http://www.cnblogs.com/feanmy/p/7676717.html

locate命令搜索的后台数据库路径:/var/lib/mlocate/mlocate.db

ls -hl /var/lib/mlocate
total 1.2M
-rw-r----- 1 root slocate 1.2M Oct 16 14:36 mlocate.db

更新数据库使用updatedb,配置文件为/etc/updatedb.conf

# 开启搜索限制
PRUNE_BIND_MOUNTS = "yes"
# 不搜索的文件系统
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs 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"

命令选项

了解一个命令可以使用man,如 man locate,可以显示locate相关的选项。这里只介绍几个常用选项
-c, --count            只输出找到的数量
-i, --ignore-case      忽略大小写
-q, --quiet            安静模式,不会显示任何错误讯息
-r, --regexp REGEXP    使用基本正则表达式
    --regex            使用扩展正则表达式
-n                     结果中显示n个文件

使用示例

1、locate /etc/pro :查询出/etc目录下所有以pro开头的文件

locate /etc/pro
/etc/profile
/etc/profile.d
/etc/protocols
/etc/profile.d/256term.csh
/etc/profile.d/256term.sh
/etc/profile.d/colorgrep.csh
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/etc/profile.d/lang.csh
/etc/profile.d/lang.sh
/etc/profile.d/less.csh
/etc/profile.d/less.sh
/etc/profile.d/vim.csh
/etc/profile.d/vim.sh
/etc/profile.d/which2.csh
/etc/profile.d/which2.sh

2、locate -c /etc/pro :显示匹配到的文件数

locate -c /etc/pro
17

3、locate -i topoftencent.class.php :忽略文件名大小写

locate -i topoftencent.class.php
/var/www/html/fxyxManage/Spider/TopOfTencent.class.php

虽然文件名大小写不一致,但使用-i选项依然能匹配出

find

基础了解

使用find进行文件查找的速度要慢于locate,但是功能及参数要强于locate。在使用find时,要避免大范围的搜索

命令选项

这里介绍常用选项,其他选项可通过man find了解

-name  filename       #查找名为filename的文件
-iname filename       #查找文件filename,不区分大小写
-gid  n           #查找属组gid为n的文件
-user  username       #按文件属主来查找
-group  groupname      #按组来查找
-mtime  -n +n        #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime  -n +n        #按文件访问时间来查GIN: 0px">
-ctime  -n +n        #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup           #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser            #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer  f1 !f2        #查更改时间比f1新但比f2旧的文件
-type             #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size   n[c]        #查长度为n块[或n字节]的文件

使用示例

1、find /etc  -name profile:在/etc目录下查找名为profile的文件

[root@iZwz985sjvpojho4jlms7lZ ~]# find /etc -name profile
/etc/profile

2、find / -user mysql :在根目录下查找属主为mysql的文件

[root@iZwz985sjvpojho4jlms7lZ ~]# find / -user apache
/proc/29936/task
/proc/29936/task/29936
/proc/29936/task/29936/attr
/proc/29936/net
/proc/29936/attr
/proc/29937/task
/proc/29937/task/29937
/proc/29937/task/29937/attr
/proc/29937/net
/proc/29937/attr
......

查看/proc/29936/task的文件属性

[root@iZwz985sjvpojho4jlms7lZ ~]# ll /proc/29936/task
total 0
dr-xr-xr-x 6 apache apache 0 Oct 16 14:12 29936 # 属主为apache

 3、find /var/lib  -group apache : 在/var/lib下根据属组apache查找文件

[root@iZwz985sjvpojho4jlms7lZ ~]# find /var/lib -group apache
/var/lib/dav
/var/lib/php/session
/var/lib/php/session/sess_7vtaesehg11f45ljrsh98k3s57
/var/lib/php/session/sess_uubh9p4tvc1mohopepndg9m3d3
/var/lib/php/session/sess_aovaqofcs4918vsl0mhquf79a7
/var/lib/php/session/sess_pm8lgnn78c9jh54umjnk0gsu14
/var/lib/php/session/sess_i1tc9i6n0chddlfp250oqun8f2
/var/lib/php/session/sess_2c1vrgb6lqgifg7lruaoq5u6s2
/var/lib/php/session/sess_3vc0gbkpl1m4hb2h5mr7nbgvk4
/var/lib/php/session/sess_rditv2odgr4vg7jmdu8sfvdm90
/var/lib/php/session/sess_a6m2fkh0cel9uf7tqo48qmphs5
/var/lib/php/session/sess_eierot422pov01ognbjo2dkqv1
/var/lib/php/session/sess_1ei3jbop7osama4m375qrsqqc5
/var/lib/php/session/sess_vovl6rrmb66a570dncpgp49p22
/var/lib/php/session/sess_6ef5d9r6fhatqie8apr28odgn0
/var/lib/php/session/sess_1o1rv6o963v3nr5sdui3hodll0

4、find /var/www -mtime -2 : /var/www目录下在2天以内修改过的文件

[root@iZwz985sjvpojho4jlms7lZ ~]# find /var/www -mtime -2
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/b595b808c2ebeeeb0478ffc30bb4e87a.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/e82f9209c88dd6a459bf8b3d6299d38c.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/dfa8fa69d0f81d1ba09048823f5e779b.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/3fd17d8cbb99d40eed8370916fbba7bf.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/32ec6c906c117a7c2bf504769276d87a.php
/var/www/html/fxyxManage/Application/Runcache/Logs/Weixin

选项-atime、-ctime的用法同-mtime

5、find /etc/httpd -type d:查找/etc/httpd下的目录,-type根据文件类型查找

[root@iZwz985sjvpojho4jlms7lZ ~]# find /etc/httpd -type d
/etc/httpd
/etc/httpd/conf
/etc/httpd/conf.modules.d
/etc/httpd/conf.d

6、find /var/www -iname topoftencent.class.php :开启不区分文件名大小写查找

[root@iZwz985sjvpojho4jlms7lZ ~]# find /var/www -iname topoftencent.class.php
/var/www/html/fxyxManage/Spider/TopOfTencent.class.php

7、find /var/lib/mysql -gid 27:查找属组id为27的文件

[root@iZwz985sjvpojho4jlms7lZ ~]# find /var/lib/mysql -gid 27
/var/lib/mysql/auto.cnf
/var/lib/mysql/ibdata1
/var/lib/mysql/ib_logfile0
/var/lib/mysql/performance_schema
/var/lib/mysql/performance_schema/accounts.frm
/var/lib/mysql/performance_schema/threads.frm
......

总结

相关推荐