linux中 locate命令
locate特定:
001:速度快
002:查找依赖于/var/lib/mlocate/mlocate.db
为了时效性查找,在使用locate命令之前可以先更新库: updatedb
示例:
1、locate + file
root@DESKTOP-1N42TVH:/home/test# locate a.txt ## 查找系统中所有包含a.txt的文件 root@DESKTOP-1N42TVH:/home/test# locate a.txt | head /home/test/a.txt /mnt/c/Program Files/Windows NT/TableTextService/TableTextServiceTigrinya.txt /mnt/c/Program Files (x86)/Internet Download Manager/Languages/tips_fa.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/AccessRuntime2019_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/AccessRuntime2021_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/AccessRuntime_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/Client2019_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/Client2021_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/ClientARMRefer2019_eula.txt /mnt/c/Program Files (x86)/Microsoft Office/root/Office16/2052/ClientARMRefer_eula.txt
2、限定查找到的文件的数目
root@DESKTOP-1N42TVH:/home/test# locate -n 3 a.txt ## 限制输出3个 /home/test/a.txt /mnt/c/Program Files/Windows NT/TableTextService/TableTextServiceTigrinya.txt /mnt/c/Program Files (x86)/Internet Download Manager/Languages/tips_fa.txt
3、 查找目录中以sh开头的文件
root@DESKTOP-1N42TVH:/home/test# locate /bin/sh ## 查找目录中以sh开头的文件 /mnt/d/programs/Git/bin/sh.exe /mnt/d/programs/Git/usr/bin/sh.exe /mnt/d/programs/Git/usr/bin/sha1sum.exe /mnt/d/programs/Git/usr/bin/sha224sum.exe
4、-i忽略大小写查找
root@DESKTOP-1N42TVH:/home/test# ls a.txt xYAbzM.txt xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate xyabzm.txt /home/test/xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate xyabzm.txt -i ## -i忽略大小写进行查找 /home/test/xYAbzM.txt /home/test/xyabzm.txt
5、-c选项统计查找到的数量
root@DESKTOP-1N42TVH:/home/test# ls a.txt xYAbzM.txt xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate xyabzm.txt /home/test/xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate -c xyabzm.txt 1 root@DESKTOP-1N42TVH:/home/test# locate -i xyabzm.txt /home/test/xYAbzM.txt /home/test/xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate -c -i xyabzm.txt ## -i表示忽略大小写, -c表示统计数目 2
6、-r选项用于正则表达式
root@DESKTOP-1N42TVH:/home/test# ls a.txt xYAbzM.txt xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate -r xy* ## 查找以xy开头的行 /home/test/xyabzm.txt root@DESKTOP-1N42TVH:/home/test# locate -r xY* ## 查考以xY开头的行 /home/test/xYAbzM.txt root@DESKTOP-1N42TVH:/home/test# locate -r zm.txt$ ## 查考以zm.txt结尾的行 /home/test/xyabzm.txt
参考:https://www.jb51.net/article/180941.htm