Open In App

Difference between locate, which and find Command in Linux

There are a number of Linux utilities that can be used to locate files in a Linux installation with three of the most common being find, locate, and which. All three of these utilities have similar functions, but work and return data in different ways. In this article, we are going to discuss these three useful and powerful command-line utilities to search for a particular file. 

 



Locate Command

locate 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 hours span. 

Syntax: 



 

locate [OPTION]... PATTERN...

For 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
  1.  

  1. Using locate findme 

     

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

     

  1. Trying locate command again locate findme 

     

  1. File location shown. Hence it is essential to update the database prior to using the locate command.

 

Which

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

For Example: 

 

which aircrack-ng

Returns the path to the aircrack-ng executable file. 

 

 

Find

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

 

#  find / -name findme

#  find / -name "*.txt"

#  find / -iname fIndMe

#  find / -type f -name findme

#  find / -type d -name find

#  find / -type f -perm 0777 -print

#  find / -type f -perm 0777 -exec chmod 755 {} \;

#  find / -mtime 10
#  find / -mtime -10

#  find / -mmin 1

#  find / -mmin -1

#  find / -size 10M

#  find / -empty

Conclusion: Final differences between locate and find 


Article Tags :