点击访问 Linux 命令大全 >>
随着文件增多,使用文件搜索工具成了顺理成章的事情。本章节将介绍怎样在 Linux 快速定位文件,主要介绍下面几个命令:
find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。语法:
find [PATH...] [EXPRESSION] [ACTION]参数说明:
PATH:要搜索的目录路径
EXPRESSION: 默认情况下(不带任何搜索条件),find 命令会返回指定目录下的所有文件,所以常常需要通过特定的 expression 对结果进行筛选。
ACTION:find 命令默认的 action 是将所有检索结果打印至标准输出(-print)。可以通过自定义 action ,让 find 命令对搜索到的结果执行特定的操作。
实例:在 /usr/bin 目录中查找 zip 命令。
[root@localhost ~]# find /usr/bin/ -name zip -print
/usr/bin/zip
[root@localhost ~]# find /usr/bin/ -name zip
/usr/bin/zip上面例子中,需要一个路径作为查找的范围,在这里是 /usr/bin/。find 命令会深入到这个目录的每一个子目录中去寻找。-name 选项指定了文件名,这里意思为“查找文件名为 zip 的文件”,-name 是允许使用通配符的,例如:find /usr/bin -name *.java -print 将列出 /usr/bin 目录下面所有 java 文件。-print 表示将结果输出到屏幕。注意,find 命令会打印出文件的绝对路径。
实例:使用 -type 选项来定位特殊文件类型。在 /etc/ 目录中查找名称为 sysconfig,类型为目录的目录。
[root@localhost ~]# find /etc/ -name sysconfig -type d -print
/etc/sysconfig-type 可以使用的参数如下:
b:块设备文件
c:字符设备文件
d:目录文件
f:普通文件
p:命令管道
l:符号链接
实例:使用 -atime n 用来查找最后一次使用在 n 天前的文件,可以用 +n 表示大于 n,或 -n 表示小于 n。
[root@localhost ~]# find /root/ -atime +1 -print
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
[root@localhost ~]#上面实例,查找 /root/ 目录下面最后一次使用大于1天的文件。
[root@localhost ~]# find /root/ -atime -1 -print
/root/
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.bash_history
/root/test1.txt
/root/test2.txt
/root/.lesshst上面实例,查找 /root/ 目录下面最后一次使用小于1天的文件。
实例:使用 -mtime n 用来查找最后一次修改在 n 天前的文件,可以用 +n 表示大于 n,或 -n 表示小于 n。
[root@localhost ~]# find /root/ -mtime +1 -print
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
[root@localhost ~]#上面实例,查找 /root/ 目录下面最后一次修改时间大于1天的文件。
[root@localhost ~]# find /root/ -mtime -1 -print
/root/
/root/.bash_history
/root/test1.txt
/root/test2.txt
/root/.lesshst
[root@localhost ~]#上面实例,查找 /root/ 目录下面最后一次修改时间小于1天的文件。
尽管 find 命令已经展现了其强大的搜索功能,但对于大批量的搜索而言,还是显得慢了一些。特别是当用户完全不记得自己的文件放在哪里的时候。这是,locate 命令会是一个不错的选择。例如:
[root@localhost ~]# locate
-bash: locate: 未找到命令上面提示没有 locate 命令,可以使用“yum list | grep locate”命令安装包。如下:
[root@localhost ~]# yum list | grep locate
mlocate.x86_64 0.26-8.el7 base然后使用“yum install mlocate”安装 locate 命令。如下:
[root@localhost ~]# yum install mlocate
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 mlocate.x86_64.0.0.26-8.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=======================================================================================================================================
Package 架构 版本 源 大小
=======================================================================================================================================
正在安装:
mlocate x86_64 0.26-8.el7 base 113 k
事务概要
=======================================================================================================================================
安装 1 软件包
总下载量:113 k
安装大小:379 k
Is this ok [y/d/N]: y
Downloading packages:
mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mlocate-0.26-8.el7.x86_64 1/1
验证中 : mlocate-0.26-8.el7.x86_64 1/1
已安装:
mlocate.x86_64 0:0.26-8.el7
完毕!这是就可以使用 locate 命令了。如下:
[root@localhost ~]# locate *.txt
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录这是因为我们没有使用“updatedb”命令更新 locate 数据库。如下:
[root@localhost ~]# updatedb继续使用 locate 命令查找所有 txt 文件。如下:
[root@localhost ~]# locate *.txt
/root/test1.txt
/root/test2.txt为什么 locate 那么快呢?locate 并没有进入子目录搜索,而是通过搜索 updatedb 命令创建的文件名数据库索引来快速查找文件。
whereis 命令主要用于查找程序文件,并提供这个文件的二进制可执行文件、源码文件和使用手册存放位置。
例如:查找 find 命令。
[root@localhost ~]# whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz可以使用 -b 选项让 whereis 命令只查找指定命令的二进制文件。如下:
[root@localhost ~]# whereis -b find
find: /usr/bin/find如果 whereis 没有找到文件,则返回一个空字符串。如下:
[root@localhost ~]# whereis finds
finds:注意:whereis 命令只搜索 /usr/bin、/usr/sbin、和 /usr/share/man 目录。