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

  2. Using locate findme

  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

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.