美文网首页linux
locate命令使用

locate命令使用

作者: theache | 来源:发表于2019-10-10 09:12 被阅读0次

    在linux下经常需要查找位置文件的路径,之前我经常用的是find。find查找文件很方便,但是在实际使用的时候。我经常是不知道查找的文件实在那个文件夹下面,需要全盘扫描。这个时候用find可能就要等上很久才能显示搜索结果。
    locate命令也是用于查找文件。与find不同的是,locate命令是提前为系统上的文件建立索引。扫描索引文件达到查找文件路径的效果。优缺点就很明显可,查找速度快,但是需要提前建立索引。

    centos使用locate

    在我的vps机器上系统是centos7.7,默认没装locate,使用下面的命令安装locate

    // 安装locate
    yum install mlocate -y
    // 更新索引
    updatedb
    // 搜索<file>
    locate <file>
    

    索引文件地址为/var/lib/mlocate/mlocate.db。索引文件并不会很大,在我使用5G磁盘的情况下生成2.6M的索引文件。所以可以安心使用

    mac使用locate

    mac是默认安装locate的,只是更新索引的命令不是updatedb,而是/usr/libexec/locate.updatedb,可以添加别名使用。

    echo "alias updatedb='/usr/libexec/locate.updatedb'" >> ~/.zshrc && source ~/.zshrc
    

    locate使用

    // 更新索引
    updatedb
    // 搜索<file>
    locate <file>
    

    在更新索引的时候可能会存在没有权限,给提示文件添加写入权限即可。

    我的mac大概用了60G的磁盘空间。查找nginx.conf文件的消耗时间比较

    // find命令查找
    find / -name nginx.conf  2.74s user 45.51s system 55% cpu 1:27.48 total
    // locate命令查找
    locate nginx.conf  0.83s user 0.03s system 94% cpu 0.913 total
    

    相关文章

      网友评论

        本文标题:locate命令使用

        本文链接:https://www.haomeiwen.com/subject/vdcopctx.html