Linux Commands

What is the Difference Between locate and find Commands in Linux

The locate and find commands are used to search a file by name in the directory. The locate command searches the files in the database and is much faster than the find command. The find command finds the files in the filesystem and it is more powerful and searches files based on different conditions.

This article is a comparison between locate and find commands in Linux systems.

find vs locate Command

In Linux systems, find and locate commands are useful and they are used for a similar purpose to search for a file in the system. Though their purpose is similar they use different methodologies for finding the files.

The following are the difference between the find and locate commands in Linux:

Find Command Locate Command
It finds the file in the filesystem It finds the file in the database
It is a little slow as compared to locate locate is much faster the Find
The data is accurate The data is sometimes not accurate
It performs real-time scanning Find files in mlocate database
It is installed on the Linux by default It must be installed using the package manager
It has a complicate syntax locate command has the simple syntax

Find the Files Using the find Command in Linux

The find command searches the files based on the size, permission, type, group, time, and date. The following is the general syntax of the command:

find <starting-directory> <options> <search term>

Search a file in a Linux with the files name using the following command:

find . -name sample.txt

To find a directory using the find command, run the following command in the terminal:

find / -type d

Execute the following find command to search for the empty files and directories:

find / -empty

Find the Files Using the locate Command in Linux

The locate command depends on the mlocate database if the database is not updated, it will not work properly. The usage of locate command is easy. You have to install it on your system via the following command:

sudo apt install mlocate

If the command is not working then update the installed database by executing the following command:

sudo updated

Once the update is finished, use the following syntax to search the file in Linux:

locate <file-name>

Here I am searching the filename.wav:

locate filename.wav

Use the -r operator with the locate command to get the exact match of the file:

locate -r txt$

Bottom Line

The locate and find commands are popular and come in handy. The locate command is helpful in searching files quickly and uses the database to search the files on the disk so it must be updated to work properly.  The find command is more powerful but it takes a long time to search the files in the filesystem.  It has several options and is more configurable.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.