Difference between locate, which and find Command in Linux
Last Updated :
17 Oct, 2024
In Linux, there are multiple utilities available to locate files efficiently within a system. Three of the most commonly used commands for this purpose are:
Each command serves a similar function but differs in the way they search and the data they return. In this article, we'll explore the differences between these commands, how they work, and examples of their usage.
Locate Command
cate is a Unix utility which serves to find files on filesystems. It searches through a prebuilt local database of all files on the filesystem generated by the updatedb command. Note it is essential to update the database as recent files saved over a period of fewer than 24 hours are not updated into the database by default and the database is updated once within 24 hour span.Â
Syntax
locate [OPTION]... PATTERN...
Example:
Let us create a file on desktop named findme.txt. Using locate command without updating the database returns no output however after updating the local database of all files on the filesystem, we are finally able to locate the file.Â
1. Creating a new file findme
echo "findme" >> findme

2. Using locate before updating the database:

3. Unable to find the file. The local database of all files on the filesystem is updated.Â

4. Trying locate command again locate findmeÂ

5. File location shown.
Hence it is essential to update the database prior to using the locate command.
Which Command
The whichcommand searches through the directories that are defined in the $PATH environment variable for a given filename. If a match is found, which returns the full path to the executable fileÂ
Syntax
which [-a] filename ...
Example:Â
which aircrack-ng
Returns the path to the aircrack-ng executable file.Â
Key Points:
- which is used to determine the location of executable programs.
- It searches through directories listed in the $PATH variable.

Find Command
The find command is a more aggressive search tool than locate or which. Find is able to recursively search any given path for various files. Using the file command we can search for files by name, owner, group, permissions, type, size, time modified, date and various other criteria. The find command is highly efficient but this efficiency comes at a cost of time.Â
Syntax
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
The -H, -L and -P options control the treatment of symbolic links. The options -H -L -P -D -O must appear before the first pathname if needed. The part of the command line after the list of starting points is the expression. This is a kind of query specification describing how we match files and what we do with the files that were matched.Â
Entering file or file. will display the pathnames of all files in the current directory and all subdirectories. The default directory is current directory and default expression is -print.Â
. represents current directory
/ represents root directoryÂ
- Searching a file by its name Â
# find / -name findme

- Searching a text file onlyÂ
# find / -name "*.txt"Â

- Searching a file ignoring case.Â
# find / -iname fIndMe

- Searching for file only. Â
# find / -type f -name findme

- Searching for directories only.Â
# find / -type d -name find

- Searching for file based on permission.Â
# find / -type f -perm 0777 -print

- Changing permission of a file.Â
# find / -type f -perm 0777 -exec chmod 755 {} \;

- Files modified more than 10 days.Â
# find / -mtime 10
- Files modified less than 10 days.Â
# find / -mtime -10

- Files modified more than 1 minute.Â
# find / -mmin 1

- Files modified less than 1 minutes. Â
# find / -mmin -1

- Files with size more than 10MB.Â
# find / -size 10M

- Search for empty files and directoriesÂ
# find / -empty

Conclusion
The locate and find commands each have their own strengths and weaknesses for finding files in a Linux system. The locate command is faster because it searches a pre-built database rather than the live filesystem, but it may not include newly added files until the database is updated.
Additionally, locate doesn't index every part of the filesystem, excluding certain paths, mounts, and file types as defined in /etc/updatedb.conf. On the other hand, find offers more accuracy by searching the filesystem live, providing up-to-the-moment results. Although find is slower, it allows greater flexibility with attribute-based searches and can be limited to specific directories to improve speed.
Similar Reads
Difference Between su and su - Command in Linux As a new Linux user, you may always face confusion regarding the difference between `su` command and `su -` command. In Linux, the `su` command is used to switch to another user account. However, there are two variations of the `su` command: `su` and `su -` (su hyphen).Table of ContentWhat is Linux
6 min read
locate command in Linux with Examples locate command in Linux is used to find the files by name. There are two most widely used file-searching utilities accessible to users called to find and locate. The locate utility works better and faster than the find command counterpart because instead of searching the file system when a file sear
6 min read
How to Display Path of an Executable File in Linux | Which Command In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th
6 min read
How to Exclude Certain Paths With the find Command in Linux The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in
3 min read
Linux - Installing locate Command to Find Files In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is a
2 min read
slocate command in Linux with Examples slocate command in Linux is used to find the files in your system. This is a secure version of the Linux command locate which is also used for file searching, similar to locate command it creates a database of the file locations for faster searching. But it is much more secure than locate command be
3 min read
Difference Between a Script file and a Binary file In this tutorial, we will learn what a script file and a binary file are and what's the difference between a Script file and a Binary file. Binary File A binary file is a file in which the content of the file is in binary format, the file data is not human-readable. Binary files contain formatted in
4 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
source Command in Linux with Examples If you're new to the world of Linux, you might have heard about commands that do various tasks, but some like the 'source' command might seem a bit confusing at first. Don't worry; let's break it down step by step.What is the Source Command?The source command in Linux is like a magic wand that lets
7 min read
dir command in Linux with examples The dir command in Linux is used to list the contents of a directory, providing an overview of the files and folders within it. How is the dir command different from ls?dir command differs from the ls command in the format of listing contents that is in default listing options. By default, dir comma
5 min read