How to Use the ‘locate’ Command in Linux

'locate' command is your go-to for fast Linux file and directory searches.

The locate Linux command is designed to search and find files and directories on your system. Unlike other search commands like find, locate offers a quicker search by accessing a database of the files and directories.

Understanding the locate command opens doors to efficient file management in Linux. Similar to the find command, but often faster, locate is used to quickly pinpoint the location of files and directories. It’s particularly handy for IT professionals, developers, and Linux enthusiasts who need to navigate complex file systems. For those looking to enhance their workflow, combining the locate command with commands like updatedb can further streamline the search process.

How to Install the locate Command

The locate command may not be installed by default on some Linux distributions. Here’s how you can install and uninstall it, sorted by distribution:

Debian-based Systems (e.g., Ubuntu)

Install:

sudo apt-get update
sudo apt-get install mlocate

Uninstall:

sudo apt-get remove mlocate

Red Hat-based Systems (e.g., Fedora, CentOS)

Install:

sudo dnf install mlocate

Uninstall:

sudo dnf remove mlocate

Arch Linux

Install:

sudo pacman -S mlocate

Uninstall:

sudo pacman -R mlocate

openSUSE

Install:

sudo zypper install mlocate

Uninstall:

sudo zypper remove mlocate

How to Use locate

1. Limiting the Number of Results

Syntax: locate -n

Explanation: Limits the number of search results.

Example: locate -n 1 myfile.txt

Output:

/home/user/documents/myfile.txt

The command has located only one instance of myfile.txt due to the limit set by the -n option.

2. Ignoring Case Sensitivity

Syntax: locate -i

Explanation: Searches for files or directories without considering case sensitivity.

Example: locate -i MyFile.txt

Output:

/home/user/documents/myfile.txt
/var/www/html/MyFile.txt

The command has located two instances of MyFile.txt ignoring the case.

3. Searching Within a Specific Directory

Syntax: locate --regex

Explanation: Searches for files or directories using regular expressions.

Example: locate --regex '/home/user/documents/.*\.txt'

Output:

/home/user/documents/myfile.txt
/home/user/documents/anotherfile.txt

The command has located all text files within the /home/user/documents directory using a regular expression.

4. Counting the Number of Matching Files

Syntax: locate -c

Explanation: Counts the number of matching files or directories.

Example: locate -c myfile.txt

Output:

2

The command has counted two instances of myfile.txt in different directories.

5. Displaying Only Files Modified Within a Certain Number of Days

Syntax: locate --time

Explanation: Displays files modified within a specified number of days.

Example: locate --time 7 myfile.txt

Output:

/home/user/documents/myfile.txt

The command has located one instance of myfile.txt that was modified within the last 7 days.

6. Displaying Statistics About the Locate Database

Syntax: locate -S

Explanation: Displays statistics about the locate database.

Example: locate -S

Output:

Database /var/lib/mlocate/mlocate.db:
  18,234 directories
  72,564 files
  2,345,678 bytes in file names
  456,789 bytes used to store database

The command has displayed statistics about the locate database, including the number of directories, files, and bytes used.

7. Using a Custom Database with Locate

Syntax: locate -d

Explanation: Uses a specified database instead of the default one.

Example: locate -d /path/to/custom/database.db myfile.txt

Output:

/custom/path/documents/myfile.txt

The command has located myfile.txt using a custom database specified by the -d option.

9. Excluding Specific Paths from the Search

Syntax: locate --exclude

Explanation: Excludes specific paths from the search results.

Example: locate --exclude /var myfile.txt

Output:

/home/user/documents/myfile.txt

The command has located myfile.txt but excluded results from the /var directory.

10. Locating a Specific File or Directory

Syntax: locate

Explanation: Finds the location of a specific file or directory.

Example: locate myfile.txt

Output:

/home/user/documents/myfile.txt
/var/www/html/myfile.txt

The command has located two instances of myfile.txt in different directories.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail