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

所属分类: 脚本专栏 / linux shell 阅读数: 1410
收藏 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上的文件搜索命令实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

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

linux创建用户useradd命令代码示例

本文通过代码示例给大家介绍了adduser与useradd命令二者的关系 以及使用useradd命令添加用户的方法,需要的朋友参考下吧
收藏 0 赞 0 分享

Linux 中LVS NAT 配置步骤的详解

这篇文章主要介绍了Linux 中LVS NAT 配置步骤的详解的相关资料,这里列出详细的实现步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux中的内核链表实例详解

这篇文章主要介绍了Linux中的内核链表实例详解的相关资料,链表中一般都要进行初始化、插入、删除、显示、释放链表,寻找节点这几个操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中特殊字符的用法总结大全

这篇文章主要给大家总结了关于Shell中特殊字符的相关资料,文中包括分好、&、#、!、$、大于号、单双引号等等一系列特殊字符的用法,通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Shell脚步攻略之管道重定向基础

管道是为了解决进程间通信问题而存在,它可以让两个进程之间的数据进行传递,将一个进程的输出数据传递给另一个进程作为其输入数据
收藏 0 赞 0 分享

Linux shell数组循环的实例详解

这篇文章主要介绍了Linux shell数组循环的实例详解的相关资料,这里举例说明如何实现shell数组循环,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell内置判断语句

内置判断,成功的时候返回0,不成功返回非零。接下来通过本文重点给大家介绍linux shell内置判断语句,感兴趣的的朋友一起看看吧
收藏 0 赞 0 分享

Linux文件的归档和压缩命令

文件归档命令tar,文件归档有好多好处,方便使用易于管理,接下来通过本文给大家分享linux文件的归档和压缩命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux Shell中curl和wget使用代理IP的方法教程

这篇文章主要给大家介绍了关于在Linux Shell中curl和wget使用代理IP的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux 日常常用指令及应用小结

最近在学习一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了
收藏 0 赞 0 分享
查看更多