Locate Command in Linux
4 min read
One of the most common operations when working on Linux is to search for files and directories. There are several commands on Linux systems that allow you to search for files, with find and locate being the most used ones.
locate command is the quickest and simplest way to search for files and directories by their names.
In this article, we will explain how to use the
locate (locate command not found)
Depending on the distribution and how the system was provisioned, the locate package may or may not be pre-installed on your Linux system.
To check whether the
locate utility is installed, open up your terminal, type
locate and press
Enter. If the package is installed, the system will display
locate: no pattern to search for specified. Otherwise, you will see something like
locate command not found.
locate is not installed, you can easily install it using the package manager of your distro.
locate on Ubuntu and Debian
sudo apt update
sudo apt install mlocate
locate on CentOS and Fedora
sudo yum install mlocate
locate command searches for a given pattern through a database file that is generated by the
updatedb command. The found results are displayed on the screen, one per line.
During the installation of the mlocate package, a cron job
is created that runs the
updatedb command every 24 hours. This ensures the database is regularly updated. For more information about the cron job check the
The database can be manually updated by running
updatedb as root or user with sudo privileges:
The update process will take some time, depending on the number of files and directories and the speed of your system.
Files created after the database update will not be shown in the locate results.
Compared to the more powerful
command that searches the file system,
locate operates much faster but lacks many features and can search only by the file name.
How to Use the
The syntax for the
locate command is as follows:
locate [OPTION] PATTERN...
In its most basic form, when used without any options, the
locate command will print the absolute path of all files and directories that matches the search pattern and for which the user has read permission.
For example to search for a file named
.bashrc you would type:
The output will include the names all files containing the string
.bashrc in their names:
/etc/bash.bashrc /etc/skel/.bashrc /home/linuxize/.bashrc /usr/share/base-files/dot.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc
/root/.bashrc file will not be shown because we ran the command as a normal user that doesn’t have access permissions to the
If the result list is long, for better readability, you can pipe the output to the
locate .bashrc | less
locate command also accepts patterns containing globbing characters such as the wildcard character
*. When the pattern contains no globbing characters, the command searches for
*PATTERN*. That’s why in the previous example, all files containing the search pattern in their names were displayed.
The wildcard is a symbol used to represent zero, one, or more characters. For example, to search for all
.md files on the system, you would type:
To limit the search results, use the
-n option followed by the number of results you want to be displayed. The following command will search for all
.py files and display only 10 results:
locate -n 10 *.py
locate performs case-sensitive searches. The
--ignore-case) option tells
locate to ignore the case and run a case-insensitive search.
locate -i readme.md
/home/linuxize/p1/readme.md /home/linuxize/p2/README.md /home/linuxize/p3/ReadMe.md
To display the count of all matching entries, use the
--count) option. The following command would return the number of all files containing
.bashrc in their names:
locate -c .bashrc
locate doesn’t check whether the found files still exist on the file system. If you deleted a file after the latest database update, and if the file matches the search pattern, it will be included in the search results.
To display only the names of the files that exist at the time
locate is run, use the
--existing) option. For example, the following would return only the existing
locate -e *.json
If you need to run a more complex search, use the
--regexp) option, which allows you to search using a basic regexp instead of patterns. This option can be specified multiple times.
For example, to search for all
.avi files on your system and ignore case, you would run:
locate --regex -i "(\.mp4|\.avi)"
locate command searches the file system for files and directories whose name matches a given pattern. The command syntax is easy to remember, and results are shown almost instantly.
For more information about all available options of the
locate command type
man locate in your terminal.
If you have any questions, feel free to leave a comment.