Use Locate command to Find Files (with Examples)

Last updated: March 4, 2024 — 4 Comments

This tutorial shows how to use locate command to quickly find files in your Linux system.

In Ubuntu Linux, the locate command is provided by the plocate package. It’s very fast command line search tool, that can find all files in the system matching the given pattern. It rarely needs to scan through its entire database, and most I/O is done asynchronously, but the results are synchronized.

Most importantly plocate is easy to use! I regularly use it to search app icon images that are in use in most pages of this website.

1. Install Plocate

Plocate is available in system repository, but not pre-installed in Ubuntu Linux

To get the tool, you need to first run command to install the package:

sudo apt install plocate

2. Update Plocate database

Installing plocate command will automatically build the database, which is by default available in /var/lib/plocate directory.

For the time being, the files vary in your system. So, you may need to manually update the database regularly by running command:

sudo updatedb

In my case in Ubuntu 22.04 Desktop, the database only takes 18 MB disk space.

3. Use locate command to find files

The locate command is quite easy to use. You can use it to find file with accurate filename. For example:

locate firefox.desktop

The command above will list all firefox.desktop files in your system, including the full path.

Sometimes, the files you’re going to search include both upper-case and lower-case letters.

In the case, you may use -i (or --ignore-case) to do case-insensitive search.

locate -i gthumb.desktop

As you see in the screenshot, the command outputs gThumb.desktop in the search results.

The locate command supports multiple patterns. When more than one are given, then it will search for files that match all of them!

For example, you can search files in certain directory by command:

locate /usr/share/icons firefox

The command above will search files under /usr/share/icons directory with firefox in filename or path.
You can add more conditions, for example the command below will search files under /usr/share/icons directory with firefox, .svg and 64x64 in either path or filename.

locate /usr/share/icons/ firefox .svg 64x64

As you see above, locate also search the given keyword from the path name (folder and directory).

To skip them, and only match against the file-name, use either -b or (--basename) flag.

locate --basename Papirus

The command above only search files include Papirus in file-names, but exclude the folders and directories (but still printed).

If you want to count how many matches, use either -c or --count option. In the case, it won’t print the files, instead only shows a total number.

For choice, you may also limit matches with -l or --limit option. In the case, it stops searching after limited number of matches have been found.

Advanced users can use regular expression as search pattern by using either -r or --regexp (or --regex for POSIX ex‐tended regular expressions).

For example, use command below to search all .png files under user’s Pictures folder, with screenshot follow with number in file-name.

locate --regexp --ignore-case $HOME/pictures .*screenshot.*[0-9].*\.png$

Summary

Ubuntu Linux provides plocate package in system repository, which includs locate command to quickly search files in the system.

User can simply run locate command follow with pattern to search in files and paths. Multiple patterns (separated with space) are allowed, and it will search for files that match all of them! It by default also search in path (folder/directory names), which can be disabled via either -b or --basename flag. Use -i or --ignore-case can do case-insensitive search in case you don’t know if there are upper-case (or lower-case) letters in filenames.

And, there are a few other options, such as --limit to limit search results, and count to print total file number. Advanced users can use -r or --regexp regular expressions to do file search in more flexible way.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

4 responses to Use Locate command to Find Files (with Examples)

  1. Thank you for sharing this.

    As a long-time previous Windows user, I was looking for something similar to Everything but for Ubuntu.
    For GUI I found FSearch but your CLI suggestion is very welcome. :)
    Thanks again for your complete tutorial.

    • I used to use FSearch. It’s great, but sadly not available in Ubuntu repository.

      As a lazy man, sometimes I don’t even want to spend time to search PPA, add it into system, and then install packages from it. So, I run few commands instead to do the job. But I like FSearch and will install it back in my new laptop, when feel free.

  2. I like Caffish File Search to find files. Great if you are a GUI person and not a terminal expert.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*