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

所属分类: 脚本专栏 / linux shell 阅读数: 1481
收藏 0 赞 0 分享

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
......

以上所述是小编给大家介绍的Linux上的文件搜索命令实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

php编译安装常见错误大全和解决方法

这篇文章主要介绍了php编译安装常见错误大全和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux base shell重定向详解

这篇文章主要介绍了Linux base shell重定向的相关资料,并用一个简明例子总结了常见用法(在第三节),需要的朋友可以参考下
收藏 0 赞 0 分享

Linux Shell 常见的命令行格式简明总结

这篇文章主要介绍了Linux Shell 常见的命令行格式简明总结,非常实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell 命令替换的两种方式

这篇文章主要介绍了Shell 命令替换的两种方式,需要的朋友可以参考下
收藏 0 赞 0 分享

Python创建、删除桌面、启动组快捷方式的例子分享

这篇文章主要介绍了Python创建、删除桌面、启动组快捷方式的例子分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell基础学习中的字符串操作、for循环语句示例

这篇文章主要介绍了shell基础学习中的字符串操作、for循环语句示例
收藏 0 赞 0 分享

shell脚本中28个特殊字符的作用简明总结

这篇文章主要介绍了shell脚本中28个特殊字符的作用简明总结,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell流程控制语句实例讲解(if、for、while、case语句实例)

linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case)。下面我将通过例子介绍下,各个语句使用方法
收藏 0 赞 0 分享

分享一个实用的iptables脚本(各种过滤写法参考)

这篇文章主要介绍了分享一个实用的iptables脚本(各种过滤写法参考),需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本实现ssh自动登录功能分享

mac下没有找到好用的类似secureCRT,就自己写了个自动登录的脚本,分享一下,如果是新浪的,就基本不用修改代码就直接能用
收藏 0 赞 0 分享
查看更多