Showing posts with label locate Command. Show all posts
Showing posts with label locate Command. Show all posts

Tuesday, 22 September 2020

Exploring the Linux locate command

Linux locate command, LPI Study Materials, LPI Learning, LPI Guides, LPI Certification, LPI Exam Prep

The Linux locate command lets you easily find files in the filesystem. It works by maintaining a system-wide database of "all files which are publicly accessible". The database itself is updated periodically by a background process. Because of this approach it returns results much faster than the find command, which only looks for files when you tell it to. Depending on your system, the locate command may need to be configured initially, or it may be pre-configured to work out of the box.

The Linux locate command is easy to use. If you want to find a file named "foo", just use this locate syntax:

locate foo

The locate command returns all files it knows whose name contains the string "foo", like "foo1", "foobar", etc.

Linux locate command, LPI Study Materials, LPI Learning, LPI Guides, LPI Certification, LPI Exam Prep

You can also use the usual filename wildcard characters, including ? and *. For instance, this locate example command will list every Java file on your system:

locate "*.java"

(Be prepared, that can be a very long list.)

Finally, you can also perform a case-insensitive search using the locate command with the -i argument, like this locate command example:

locate -i "*.java"

Saturday, 27 June 2020

Linux ‘locate’ command examples

Linux Tutorial and Material, Linux Cert Exam, Linux Certification, Linux Learning

Linux FAQ: Can you share some examples of how to use the Linux locate command?

Background


Sure. The locate command is used to find files by their filename. The locate command is lightning fast because there is a background process that runs on your system that continuously finds new files and stores them in a database. When you use the locate command, it then searches that database for the filename instead of searching your filesystem while you wait (which is what the find command does).

To find files on Unix and Linux systems, I've historically fired up my old friend, the Linux find command. For instance, to find a file named tomcat.sh, I used to type something like this:

find / -name tomcat.sh -type f

This is a lot of typing, and although the results are current, it takes a long time to run on a big system. Recently a friend told me about the Linux locate command, and I haven't looked back since.

Using the locate command


Using the command is easy; just type locate followed by the name of the file you're looking for, like this:

locate tomcat.sh

Or, add the -i option to perform a case-insensitive search, like this:

locate -i springframework

Your Linux system will very quickly tell you all of the places it has been able to find (locate) the file with the name you specified. I emphasize the "very quickly" part, because this is so much faster than using the Linux find command you'll never look back easier.

Another thing to note: if you type in a name like foo (or any other name) locate, by default, returns the name of any file on the system that contains the string "foo", whether. So whether the filename is foo, or foobar, or barfoo, locate will return all of these as matches.

How the Linux locate command works


The locate command works so fast because it runs a background process to cache the location of files in your filesystem. Then, when you want to find the file you're looking for, you can just use the command like I showed previously. It's that easy. Here's a quick blurb from the locate man page:

The locate program may fail to list some files that are present, or may list files
that have been removed from the system.  This is because locate only reports files
that are present in the database, which is typically only regenerated once a week
by the /etc/periodic/weekly/310.locate script.

Use find(1) to locate files that are of a more transitory nature.

The locate database is typically built by user "nobody" and the locate.updatedb(8) utility skips directories which are not read-able for user "nobody", group "nobody", or world.  For example, if your HOME directory is not world-readable, none of your files are in the database.

So, as the man page states, if you can't find a file using the locate command, it may be that the database is out of date, and when this happens, you can always use the find command. The find command will be slower because it scans all the files you specify in real time (it doesn't have a database backing it up), but you can also perform more powerful searches with it.