Skip to content

Locate Linux Command Guide

The Linux locate command allows users to quickly and efficiently search for files on their system using a pre-built index. By utilizing the locate command, users can save time by swiftly locating the desired file without having to manually search through directories.

locate Syntax:

Terminal window
locate [option] [pattern]

Options:

OptionDescription
-A, —allPrint only entries that would be output
-c, —countPrint only the number of matching entries
-i, —ignore-caseIgnore case distinctions
-r, —regexpInterpret PATTERN as a regular expression
-w, —wholenameMatch whole path name
-h, —helpDisplay help information
-V, —versionDisplay version information

Parameters:

ParameterDescription
patternThe pattern to search for in the locate database

locate Command Samples:

Search for a file

Terminal window
locate filename.txt

Locates the file named “filename.txt” on the system.

Search for files with specific extension

Terminal window
locate *.log

Locates all files with the “.log” extension on the system.

Update the locate database

Terminal window
sudo updatedb

Updates the locate database to ensure that the search results are current.

Search for files ignoring case sensitivity

Terminal window
locate -i examplefile.txt

Locates the file named “examplefile.txt” while ignoring case sensitivity.

Display the path of found files

Terminal window
locate -r /home/user/examplefile.txt

Locates and displays the path of the file named “examplefile.txt” within the specified directory.

Terminal window
locate -c *.jpg

Locates and counts all files with the “.jpg” extension on the system.

Search for a file and limit the number of results

Terminal window
locate -n 5 filename.pdf

Locates the file named “filename.pdf” but limits the search results to display only the first 5 matches.

locate FAQ:

How do I use locate in Linux?

To use the locate command in Linux, execute the following command:

Terminal window
locate --name file.txt

What is the purpose of the locate command in Linux?

The locate command is used to quickly locate files by searching a pre-built database for file names.

How can I make locate search case-insensitive in Linux?

To make the locate command search case-insensitive, use the -i option as shown in the example below:

Terminal window
locate -i FILE.txt

How can I display the number of matches found by the locate command in Linux?

To display the number of matches found by the locate command, use the -c option as shown below:

Terminal window
locate -c file.txt

How can I limit the search depth of the locate command in Linux?

To limit the search depth of the locate command, use the -d option followed by the desired depth level as shown in the example below:

Terminal window
locate -d 3 file.txt

How can I exclude specific directories from the search results in the locate command in Linux?

To exclude specific directories from the search results, use the -e option followed by the path of the directories to be excluded as shown below:

Terminal window
locate -e /home/user/exclude_dir file.txt

Applications of the locate command

  • Quickly find files and directories on the system
  • Search for specific files based on their names
  • Locate files and directories without knowing their exact paths