Open In App

Difference between locate, which and find Command in Linux

Last Updated : 22 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

 

  • 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: Final differences between locate and find 

  • There are advantages and disadvantages to using locate to find filenames instead of the find command. A locate command finds files faster because it searches a database instead of having to search the whole filesystem live. A dis-advantage is that the locate command cannot find any files added to the system since the previous time the database was created. 
     
  • Not every file in your filesystem is stored in the database. The contents of the /etc/updatedb.conf file limit which filenames are collected by pruning out select mount types, filesystem types, file types, and mount points. For example,filenames are not gathered from remotely mounted filesystems (cifs, nfs, and so on) or locally mounted CDs or DVDs. Paths containing temporary files(/tmp) and spool files (/var/spool/cups) are also pruned. You can add items to prune (or remove some items that you don’t want pruned) the locate database to your needs. 
  • As a regular user, you won’t be able see any files from the locate database that you couldn’t see in the filesystem normally. For example, if you can’t type ls to view files in the /root directory, you won’t be able to locate files stored in that directory. 
  • If you add files to your system after updatedb runs, you won’t be able to locate those files until updatedb runs again. To get the database to contain all files up to the current moment, you can simply run updatedb from the shell as root. 
  •  
  • The find command is the best command for searching your filesystem for files, based on a variety of attributes.When you run find, it searches your filesystem live, which causes it to run slower than locate, but gives you an up-to-the-moment view of the files on your Linux system.However, you can also tell find to start at a particular point in the filesystem, so the search can go faster by limiting the area of the filesystem being searched. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads