Shell - locate file command

The Linux system is composed of many files, and these files are organized through the file system. Its structure is inherited from Unix-like systems, developed from generation to generation, and has certain conventions.

What we are facing is a huge number of files in the Linux system, which brings us difficulties in finding files.

So in order to find the file we want, the two most commonly used tools are: locate and find.

The locate program does a quick search in the database based on the pathname, and outputs each name that matches the given substring. For example, we want to find all programs whose names begin with zip. Since we're looking for programs, we can assume that the names of directories containing programs end in bin/.

Therefore, we can try to find the file using the following method:

$ locate bin/zip

/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit

The locate command searches the pathnames of its database and outputs all paths containing the string "bin/zip".

If the search requirements are more complex, we can use combination commands, such as grep, to design more powerful searches.

$ locate zip | grep bin

/bin/bunzip2

/bin/bzip2

/bin/bzip2recover

/bin/gunzip

/bin/gzip

/usr/bin/funzip

/usr/bin/gpg-zip

/usr/bin/preunzip

/usr/bin/prezip

/usr/bin/prezip-bin

/usr/bin/unzip

/usr/bin/unzipsfx

/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit

The locate program has been around for many years, with several commonly used variants. The two most common versions in modern Linux distributions are slocate and mlocate, although they are usually accessed through a symbolic link called locate. Different versions of locate have overlapping option sets. Some versions include regular expression matching and wildcard support. Please check the locate man page on your system to determine which version of locate is installed.

Where does the locate database come from?

You may notice that with some Linux distributions, running the locate command is useless after you have just installed the system, but if you try again the next day, it will work fine. What is the reason?

The locate database is created by another program called updatedb. Usually, it is a cron job (job) that runs periodically, that is, a task that is periodically executed by a cron daemon (daemon process). Most systems with locate run updatedb once a day. Since the update of the database is not real-time, when using locate, you will find that the latest files cannot be displayed. To work around this, run the updatedb program manually by becoming superuser and running updatedb at the prompt.

Guess you like

Origin blog.csdn.net/guoqx/article/details/132054500