Sunday, April 14, 2024
HomeHow toUse Locate command to Find Files (with Examples)

Use Locate command to Find Files (with Examples)

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 built the database, which is 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.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


spot_img

Most Popular