Linux使用locate命令定位文件


全文节选翻译自:http://linux-blog.org/finding-files-with-locate/

FIND命令

很多Linux用户喜欢使用find命令来查找文件,例如他们通常喜欢这样做:

find / -name 'pattern'

确实find的强大功能不仅仅用来查找文件,它能用来定位更加细节的东西,比如你想在某个目录下找到一些赋予其拥有者和管理员可写的权限( if you wanted to find files which are writable by both their owner and their group),可以这样做:

find / -perm -444 -perm /222 ! -perm /111

又或者你想看看在你的下载目录下的过去24小时之内被修改过的文件,

find /home/user/Downloads/ -mtime 0

find命令的强大不仅仅用于查找文件,其搜索磁盘的时候是需要时间的。那么有没有快速定位的方法呢?

LOCATE命令

在使用locate之前,你得确认系统是否安装了mlocate 包,现在大多数Linux发行版都集成了此包。如果没有安装,可以访问mlocate主页(http://carolina.mff.cuni.cz/~trmac/blog/mlocate/)下载安装。下载安装成功后,你需要执行一条命令为你的文件系统索引。否则你就得等到程序哪天自动执行了。

已root用户身份执行如下命令:

updatedb &

这条命令会后台更新你的mlocate数据库,这数据库包含了你的文件系统的索引(This updates the mlocate database that indexes your files and forks it to the background (the ‘&’ forks it to the background)。

等这条命令执行完了,你可以轻松的使用locate命令:

locate firefox | less

这会快速地定位有关firefox的文件、目录等等。速度也比find快,因为它读取的是mlocate数据库里面的索引!

相关内容