Difference between find and locate command in Linux
In Linux there are many methods by which a user can locate files and directories. Two of the most popular utilities for finding files and directories are: 1. find 2. locate
The locate Command
This utility performs a search through a previously created database of files and directories available on our system. It matches all entries that contain a specified character string. So before we can use the “locate” command we must first execute the updatedb command to build a local database for each file within our filesystems on Linux. This process usually takes a few seconds to complete.
Now in order to search a file or directory, simply use the below syntax.
# locate [file or directory name]
# locate file01 /var/tmp/file01
The locate command uses a database created by another program, updatedb. Most Linux systems run this program automatically once a day using a scheduler. However, we can update the locate command database any time by running the updatedb command on a Terminal.
Check Man page of “locate” command to get more details:
# man locate
The find Command
The find utility is a bit more aggressive than locate and which. The find utility will return all the results that contain the keyword or string we have specified. So when you search for a file “file01” with find command, it will return all files and directories that contain the word “file01”.
The Basic syntax of a find command is as follows:
# find [pathnames] [conditions]
Unlike locate command, there are multiple options available in find command. Below is one of the simplest find command to search for file “file01” under the directory /var.
# find /var -name file01 /var/tmp/file01
Check Man page of “find” command to get more details:
# man find
Both the command “find” and “locate” are handy tools to search for files and directories in Linux. With that said, locate is bit fast than find. But it needs to keep its database updated to provide you with the most accurate results. Whereas find has more options which cater to user to find exact file/directories and perform more operations like delete on the found files/directories. Also find has no database to update, making the results current.