There are a few ways you can find and locate files from the terminal in Linux, 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.
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.
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.
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.
Let’s assume a script has been created in the home directory.
If we attempt to locate the newly-created and copied script, we would not get any output.
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
If we attempt to “locate” the custom script, it will be able to locate the file.
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:
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.
-a switch displays all occurrences of found absolute paths for the specified executable.
The absolute paths for multiple executables can be found by specifying the executables one after another.
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
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.
Similarly, the search can be restricted to only source code or only manuals, using
-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.
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.
Our latest tutorials delivered straight to your inbox