In addition to using graphical file managers, users can employ various methods to search for files and directories on Linux. Mytour explains how to find files and directories on Linux using command-line tools after quickly accessing the Terminal.
Using the Find Command
The find command allows users to search for files and directories on Linux based on precise file names they know. Essentially, the find command searches for files in the current directory and recursively through subdirectories that match the specified search criteria. Users can search for files based on name, file owner, group, type, permissions, date, and other criteria.
Enter the command below in the Terminal window to list all files found in the current directory:
find .
The dot after the find command indicates searching in the current directory.
To discover files matching a specific pattern, utilize the -name parameter. You can employ wildcard characters in file names (such as *), but ensure to escape each character (\) before the file name or enclose the filename in double quotes.
For instance, if you wish to find all files starting with 'pro' in the Documents directory, use the cd Documents/ command to navigate to the Documents folder and execute the following command:
find . -name pro\*
All files in the current directory starting with 'pro' will be listed.
Please note: The find command, by default, is case-sensitive. If you want to search for a word or phrase case-insensitively, use the -iname option with the find command.
If no files match your criteria, the command will produce no output.
The find command offers numerous options to fine-tune your search. For more information on the find command, type man find in the Terminal window and press Enter.
Utilize the Locate command
The locate command is faster than find because it utilizes a pre-built database, while the find command searches the live system, traversing all actual directories and files. The locate command returns a list of all path names containing the specified character groups.
The database is regularly updated by cron, but you can update it anytime to refresh search results. To do this, enter the following command in the Terminal window:
sudo updatedb
Enter your password when prompted.
In essence, the locate command identifies all files in the file system, starting from the root, containing all or any part of the search criteria.
locate mydata
For example, the above command finds 2 files containing 'mydata' and 1 file containing 'data'.
If you want to search for all files or directories precisely matching and only containing your search criteria, use the -b option with the locate command:
When installing mlocate, the /usr/bin/locate binary file will be modified to point to mlocate. To install mlocate, if it is not already in your Linux distribution, enter the following command in the Terminal window:
sudo apt-get install mlocate
The mlocate command does not use the same database file as the standard locate command. So, if you want to manually create the database, enter the following command:
sudo /etc/cron.daily/mlocate
The mlocate command remains inactive until the database is manually created, typically when the script is executed through cron.
For more information about the locate or mlocate command, enter man locate or man mlocate in the Terminal window and press Enter.
Using the Which Command
The which command returns the absolute path of the called executable file. This is useful for locating the position of an executable file to create a shortcut for the program on a computer, control panel, or other locations in the desktop manager. For example, enter the command which firefox to display results as shown below:
By default, the which command displays only the first matching executable file. To show all matching executable files, use the additional option -a with the command:
which -a firefox
Alternatively, you can search for multiple executable files by adding them simultaneously, as shown below. Only paths to the found executable files are displayed. In the example below, only the executable file 'ps' is found.
Note: The which command only searches the PATH variable of the current user. If searching for executable files available to the root user as a regular user, no results will be displayed.
For more information about the which command, enter man which in the Terminal window and press Enter.
Using the Whereis Command
The whereis command is utilized to find the location of binary files, source, and man pages for a command. For example, enter the command whereis firefox in the Terminal window, and the result will be displayed as shown below:
If you only want to display the path to the executable file, rather than the path to source and man pages, use the -b option. For instance, the command whereis -b firefox will only display/usr/bin/firefox as the search result. This is particularly useful as, in most cases, you only need to search for the executable file of programs rather than searching for their source and man pages. Additionally, you can search for open-source files (-s) or only search for man pages (-m).
To discover more about the whereis command, type man whereis in the Terminal window and press Enter.
The Distinction Between the Whereis and Which Commands
The whereis command reveals the locations of binary, source, and man pages for a command, while the which command only displays the binary location of the command.
The whereis command searches through a specific list of directories for binary, source, and man files, whereas the which command searches through directories listed in the PATH environment variable. With the whereis command, the specific directories can be found in the FILES section on the command's man page.
Discussing default display results, the whereis command will showcase everything it finds, whereas the which command displays only the first executable file it discovers. This behavior can be altered using the -a option introduced by Mytour to search for files and directories on Linux.
Since the whereis command relies on hardcoded paths within the command, you may not always see what you are looking for. If searching for a program that you believe might be installed in a directory not listed in the whereis command's man page, you can use the which command with the -a option to search for all occurrences of the command throughout the entire system.
So, the Mytour article has just guided you through 4 ways to find files and directories on Linux. If you have any questions needing clarification, feel free to leave your comments below the article, and Mytour will address your queries as soon as possible.
