Exploring the "locate" command

By Alvin J. Alexander, devdaily.com

The Unix 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 locate command is easy to use. If you want to find a file named "foo", just type this:

locate foo

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

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

locate "*.java"

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

Related commands

As mentioned, the find command can also be used to find files and directories.


devdaily logo