Different Ways to Search Files or Directories on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

In Linux, you have multiple tools that can help you find the absolute path for files and directories in your system.

Today, you’ll learn about those tools from basic to advanced, including their pros and cons to help you easily distinguish between them and decide which one is right for a certain situation.

Tutorial Details

DescriptionSearching Files or Directories in Linux
Difficulty LevelModerate
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteswhich, whereis, locate, find
Internet RequiredYes (for the locate command)

Which Command: Locate a Program in the User’s Path

The which command is a basic tool to find the absolute path of executable commands or tools that can be executed from your terminal.

For example, if you want to find the absolute path of the ls command, specify it in the which command.

$ which ls

Output:

Locating command binary file using which command

In some cases, the same binary file might exist in multiple locations, which you can search for using the β€œ-a” flag.

$ which -a ls

Output:

Listing command all binaries path

The following are pros and cons of this tool:

ProsCons
Useful for finding the absolute path for executable files or tools that can be executed from your terminalLimited to executable files or tool searches
Locate the path of binary files or tools that exist in multiple locationsSupport only one β€œ-a” flag
Consume less memory
When should I use the which command?

I personally prefer to use the β€œwhich” command only when I want to find the absolute path of the executable commands or tools that can be executed from the terminal because it cannot search for anything else, which makes the searching very fast.

I recommend you read our article on which command to learn more about.

Whereis Command: Locate the Binary, Source, and Manual Page Files for a Command

The whereis command is identical to the which command except it will also provide you the location of the binary, source, and manual page.

For example, use the following command to find the absolute path of binary, source, and manual page for the ls command.

$ whereis ls

Output:

Locating command information using whereis command

The following command will only print the executable or binary file for the specified command.

$ whereis -b ls

Output:

Locating command only binary path using whereis command

If you want to search for the source file, use the following command.

$ whereis -s ls

Output:

Locating command only source path using whereis command

Lastly, use the following command to print only the manual page files.

$ whereis -m ls

Output:

Locating command only manual path using whereis command

The following are pros and cons of this tool:

ProsCoins
Useful for finding the binary, source, and manual page files for the specified commandLimited to certain searches
You will experience the same searching speed as which commandDo not support files whose path is not assigned in the environmental file
You can easily find the unusual files (referring to files with multiple manual pages)
Provides a flag to search in the specific directory that is not specified in the environmental file
When should I use the whereis command?

Whereis is one of those commands that I do not use regularly, means how many times you might be interested in knowing the location of binary, source, and manual page file paths for executable commands or tools. Yet it is a handy tool that can be used when required.

I recommend you read our article on whereis command to learn more about.

Locate Command: Find Filenames Quickly

The locate command is totally different from the previous two commands, it searches the specified files or folders within the indexes in the database file known as updatedb.

It will search at blazing speed because the database already has all the records, but you must also know that the updatedb database is set to automatically update after 24 hours using the cron jobs.

If you made any changes between this time frame, you may not find the files that you recently added or deleted file might still appear in the search result (it can be avoided using the flags) until the database is automatically or manually updated.

To use this tool, you can specify the file or directory name as an argument, and it will list all the results.

$ locate nginx

Output:

Listing all paths containing nginx name using locate command

Use the following command to display the total number of matches instead of their names.

$ locate -c nginx

Output:

Counting all paths containing nginx name using locate command

The following are pros and cons of this tool:

ProsCons
Search for all files and directories in super speed that are indexed in the databaseOutdated databases might give you the wrong results
Support for exact file matching, ignoring case-sensitivity, extensions, and typeIt does not support searching for files based on their modification date, creation date, access time, and many more
Consume less memory
When should I use the locate command?

This tool is very helpful if you have a low-end PC. It will search for your specified files or directories at blazing speed without consuming too much system memory. However, if you perform a lot of file changes, it will become boring to update the database each time you make any changes.

I recommend you read our article on the locate command to learn more about it.

Find Command: Find Files or Directories Recursively Under the Specified Directory Tree

The find command is an advanced tool that is a little bit complex for beginners but that can help you find any files or directories in your system rigorously by going through each file or directory on every search.

Note that it does not use any databases to search for indexes, but instead walks through all files and directories in your file system on every search, consuming more system memory.

Now, if you want to search for specific files in your home directory, you can use the following command.

$ find /home/linuxtldr/ -name file.txt 

Output:

Searching for file using find command

Instead of file, if you want to search for directories, you can use the following command.

$ find /home/linuxtldr/ -name Downloads

Output:

Searching for directory using find command

Note that if the file with the same name specified for the directory exists in the specified path, it might also appear in the result; ignore that and specify the type using the β€œ-type” flag.

$ find -type d -name Downloads

Output:

Searching only for directory using find command

The following are pros and cons of this tool:

ProsCons
An advanced searching tool that can recursively search for a specified file or directory in the file system treeIt will consume more system memory
Support multiple search types like modification date, creation date, accessed type, size, permissions, and many more
Have everything that one searching tool must have
When should I use the find command?

For most of the time I use find command due to its nature and the features that it provides. It is a very helpful tool, and I must recommend everyone to at least have a basic understanding of this tool.

I recommend you read our article on the find command to learn more about it.

That was the end.

If you have any more tools that need to be added to this list, feel free to comment them down.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.