Linux上的⽂件搜索命令实例详解
locate
基础了解
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,配置⽂件为/f
# 开启搜索限制
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 r # 不搜索的⽂件类型
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/dules.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/autof
/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
find查命令的使用
......
总结
以上所述是⼩编给⼤家介绍的Linux上的⽂件搜索命令实例详解,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。