Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the locate command on Linux system

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the locate command of the Linux system, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the Linux system, the locate command can help users quickly find documents that meet the requirements. The method is to first establish a database that includes the names and paths of all files in the system, and then when looking for it, you only need to query this database, without actually going deep into the file system.

Syntax locate [- d] [--help] [--version] [template style.] Parameter:-b,-basename-matches only the base name of the pathname

-c,-count-output only the number found

-d,-database DBPATH-uses the database specified by DBPATH instead of the default database / var/lib/mlocate/mlocate.db

-e,-existing-prints only entries for the current existing file

-1-if it is 1. Then start safe mode. In safe mode, users do not see files that cannot be seen by permissions. This starts to slow down because locate must go to the actual file system to obtain file permission data.

-0,-null-A separate entry with NUL on the output

-S,-statistics-do not search for entries and print statistics about each database

-Q-quiet mode, no error message is displayed.

-P,-nofollow,-H-check the existence of the file without following the trailing symbolic link

-l,-limit,-n LIMIT-limits the output (or count) to LIMIT entries

-n-displays at most n outputs.

-m,-mmap-ignored for backward compatibility

-r,-regexp REGEXP-use basic regular expressions

-regex-use extended regular expressions

-Q,-quiet-quiet mode, no error message is displayed

-s,-stdio-ignored for backward compatibility

-o-specifies the name of the data inventory.

-h,-help-Show help

-I,-ignore-case-ignore case

-V,-version-displays version information

Case demonstration: find a related file that contains a string

[root@localhost] # locate ifconfig / home/oracle/app/oracle/product/11.2.0/dbhome_1/sysman/admin/scripts/ifconfig.sh / usr/libexec/hypervkvpd/hv_set_ifconfig / usr/sbin/ifconfig / usr/sbin/pifconfig / usr/share/man/de/man8/ifconfig.8.gz / usr/share/man/fr/man8/ifconfig.8.gz / usr/share/man/man8/ifconfig.8.gz / usr/share/man/man8/pifconfig. 8.gz / usr/share/man/pt/man8/ifconfig.8.gz / usr/share/man/zh_CN/man8/ifconfig.8.gz [root@localhost ~] # search for all files that begin with sh in the directory

[root@localhost ~] # locate / bin/sh / home/oracle/app/oracle/product/11.2.0/dbhome_1/perl/bin/shasum / usr/bin/sh / usr/bin/sha1sum / usr/bin/sha224sum / usr/bin/sha256sum / usr/bin/sha384sum specify the number of displays

If you display too much content, you can use the-n option to limit the number of displays.

[root@localhost ~] # locate-n 3 passwd / etc/passwd / etc/passwd- / etc/pam.d/passwd [root@localhost ~] # ignore case differences when matching patterns

Use the-I option when lookups do not need to be case sensitive.

[root@localhost ~] # locate-I-n 5 passwd / etc/passwd / etc/passwd- / etc/pam.d/passwd / etc/security/opasswd / home/oracle/app/oracle/product/11.2.0/dbhome_1/apex/images/htmldb/icons/32x32/item_passwd.gif [root@localhost ~] # search the basic regular expression REGEXP instead of the pattern

When you need to find information that meets a particular rule, you can use the-r option to match the corresponding regular expression.

/ / find the file that begins with / var/lib/rpm [root@localhost ~] # locate-r ^ / var/lib/rpm/ var/lib/rpm/ var/lib/rpm-state / var/lib/rpm/.dbenv.lock / var/lib/rpm/.rpm.lock / var/lib/rpm/Basenames / var/lib/rpm/Conflictname / var/lib/rpm/Dirnames / var/lib/rpm/Group / var/lib/rpm/Installtid / / at the end of etc.conf File [root@localhost ~] # locate-r etc.conf$ / usr/lib/tmpfiles.d/etc.conf [root@localhost ~] # find recently changed files

# New file [root@localhost ~] # touch new_file [root@localhost ~] # locate ~ / new_file [root@localhost ~] # updatedb [root@localhost ~] # locate ~ / new_file / root/new_file [root@localhost ~] # # delete file [root@localhost ~] # rm-rf file.txt [root@localhost ~] # locate ~ / file.txt / root/file.txt [root@localhost ~] # updatedb [root@localhost ~] # Locate ~ / file.txt [root@localhost ~] # Note: you cannot find a recently changed file using the locate command. To avoid this, you can manually update the database using the updatedb command before using locate.

View statistics

[deng@localhost test] $locate-S database / var/lib/mlocate/mlocate.db: 18935 folder 231751 bytes in file 13753723 file name 5707750 bytes are used to store the database [deng@localhost test] $View passwd statistics

[root@localhost /] # locate-c passwd 183 [root@localhost /] # update related configuration file

[root@localhost /] # vim / etc/updatedb.conf [root@localhost /] # these are all the contents of the article "how to use locate commands in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report