[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Knowledge BaseApril 11, 2023

How to use Locate Command.

How To Use Locate Command.

In this article, we will explain how to use the locate command.

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.

The locate command is the quickest and simplest way to search for files and directories by their names.

 

Installing 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.

If locate is not installed, you can easily install it using the package manager of your distro.

Install locate on Ubuntu and Debian

sudo apt update sudo apt install mlocate

Install locate on CentOS and Fedora

sudo yum install mlocate

 

How Does locate Work

The 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 /etc/cron.daily/mlocate file.

The database can be manually updated by running updatedb as root or user with sudo privileges:

sudo updatedb

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 find 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 locate Command

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:

locate .bashrc

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

The /root/.bashrc file will not be shown because we ran the command as a normal user that doesn’t have access permissions to the /root directory.

If the result list is long, for better readability, you can pipe the output to the less command:

locate .bashrc | less

The 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:

locate *.md

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

By default, locate performs case-sensitive searches. The -i (–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 -c (–count) option. The following command would return the number of all files containing .bashrc in their names:

locate -c .bashrc
6

By default, 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 -e (--existing) option. For example, the following would return only the existing .json files:

locate -e *.json

If you need to run a more complex search, use the -r (–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 .mp4 and .avi files on your system and ignore case, you would run:

locate --regex -i "(\.mp4|\.avi)"

 

Conclusion

The 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.

Keep reading
How to use csplit command in Linux.
September 15, 2023

How to use csplit command in Linux.

How To Use Csplit Command. The csplit command is used to split any file into many parts as required by the user. The parts are determined by context lines. Output pieces of FILE separated by PATTERN(s) to files ‘xx00’, ‘xx01’, …, and output byte counts of each piece to standard output. Syntax:…

Read article
How to use chkconfig command.
September 15, 2023

How to use chkconfig command.

How To Use Chkconfig Command. This guide will show you how to use the chkconfig command to control system services in Linux. Modern Linux distributions come with the systemd initialization system that allows users to manage services with the systemctl command. However, some older systems still use…

Read article
How to use cfdisk command in Linux.
September 14, 2023

How to use cfdisk command in Linux.

How To Use Cfdisk Command. cfdisk command is used to create, delete, and modify partitions on a disk device. It displays or manipulates the disk partition table by providing a text-based “graphical” interface. cfdisk /dev/sda See the available free space. Here we have 20 GB. Select NEW and create…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00