Using find, locate, which and whereis Commands to Search for Files in Linux

Bg1

There are a few ways you can find and locate files from the terminal in Linux, and find, locate, which and whereis are some of the commonly used commands to do so. Let us take a look at these four search commands, their similarities and differences.

1. find

The straightforward approach to search for files within any specified directory is to use the find command. If the directory to perform the search is not explicitly specified, then the search will be performed on the current directory.

The following screenshot shows how within the current directory (.), a search is performed for all filenames ending with extension .txt.

Compare Find

The search can also be performed based on timestamps, file permissions, file size, file type, file owner, etc. Regular expressions can be used to control the search parameters. More detailed instructions on the find command can be found here.

2. locate

This command is another way to find files by name. The previous command searches the specified directory and then provides the results to the user. This tool performs the search against a database called “mlocate.db,” which is located in “/var/lib/mlocate/mlocate.db.” This database is updated every morning by the cron utility.

The command executes more quickly than find because the search is against an existing database that has already curated the list of all files and directories on the system.

locate followed by the file name displays the absolute path name where that file exists.

Compare Locate File

Let’s assume a script has been created in the home directory.

Compare Locate Custom Script

If we attempt to locate the newly-created and copied script, we would not get any output.

locate myscript
Compare Locate None

As cron only updates the database in the morning, any files are added to the system during the day, then the database needs to be manually updated. This can be done using the updatedb command.

sudo updatedb

If we attempt to “locate” the custom script, it will be able to locate the file.

Compare Locate Updated

3. which

After discussing commands to search for files, let us shift to one command that can help search for the absolute path of executables on the system: which.

One executable/script/binary may be present in multiple locations in the system. which searches in directories specified in $PATH and $MANPATH environment variables for the existence of the specified executable.

Without any switches, which displays the first absolute path found for an executable.

Compare Which Lessfile

-a switch displays all occurrences of found absolute paths for the specified executable.

Compare Which Alessfile

The absolute paths for multiple executables can be found by specifying the executables one after another.

Compare Which 2

4. whereis

whereis is another command and is used to get three pieces of information regarding an executable:

  • absolute path of the binary
  • absolute path where source code of that binary exists on the system
  • absolute path of the manual that exists for that binary
Compare Whereis

For “bzgrep,” the binary exists in “/bin,” and the manual exists in “/usr/share/man/man1.” Its source code does not exist on the system.

whereis can be instructed to search only for the absolute path of the binary using -b switch. The search may be performed only in the directories listed after -B switch. Names after -f specify all the binaries for which the information needs to be obtained.

Compare Whereis Switchsearch

Similarly, the search can be restricted to only source code or only manuals, using -s and -m switches. -S and –M followed by directory names specifies the directories to search for source code and manuals respectively.

-l switch will provide a detailed listing of the absolute paths of all directories used by whereis to perform the search.

Compare Which Listing

Conclusion

There is no lack of tools for you to find and locate files in the terminal. I hope you have a good idea now about the four useful search tools on Linux systems. If you need more details on their usage, you can check out their manual pages for a detailed documentation.

Divya Lakshmanan

Divya divides her time between speculating the existence of aliens and writing about her technical findings.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox