You can use a graphical file manager to find files in Linux, such as Nautilus in Gnome, Dolphin in KDE, and Thunar in Xfce. However, there are several ways to use the command line to find files in any Linux desktop manager.
Want to Learn More? Get Linux Training from the Linux Foundation
Learn everything you could want to know about Linux with comprehensive online Linux courses and certification tests that include the real-world experience and first-hand knowledge of what it takes to be a successful Linux developer or system administration professional, all from a web browser in the comfort of your home.
The Linux Foundation is the nonprofit consortium dedicated to fostering the growth of Linux. Founded in 2000, they are supported by the leading technology companies and developers from around the world.
Using the Find Command
The “find” command allows you to search for files for which you know the approximate filenames. The simplest form of the command searches for files in the current directory and recursively through its subdirectories that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria.
Typing the following command at the prompt lists all files found in the current directory.
The dot after “find” indicates the current directory.
To find files that match a specific pattern, use the “-name” argument. You can use filename metacharacters (such as “*”), but you should either put an escape character (“\”) in front of each of them or enclose them in quotes.
For example, if we want to find all the files that start with “pro” in the Documents directory, we would use the “cd Documents/” (without the quotes) command to change to the Documents directory, and then type the following command.
find . -name pro\*
All files in the current directory starting with “pro” are listed.
NOTE: The find command defaults to being case sensitive. If you want the search for a word or phrase to be case insensitive, use the “-iname” option with the find command. It is the case insensitive version of the “-name” command.
If find doesn’t locate any files matching your criteria, it produces no output.
The find command has a lot of options available for refining the search. For more information about the find command, type “man find” (without the quotes) at the command prompt in a Terminal window and press Enter.
Using the Locate Command
The locate command is faster than the find command because it uses a previously built database, whereas the find command searches in the real system, through all the actual directories and files. The locate command returns a list of all path names containing the specified group of characters.
The database is updated periodically from cron, but you can also update it yourself at any time so you can obtain up-to-the-minute results. To do this, type the following command at the prompt.
Enter your password when prompted.
The basic form of the locate command finds all the files on the file system, starting at the root, that contain all or any part of the search criteria.
For example, the above command found two files containing “mydata” and one file containing “data.”
If you want to find all files or directories that contain exactly and only your search criteria, use the “-b” option with the locate command, as follows.
locate -b ‘\mydata’
The backslash (‘\’) in the above command is a globbing character, which provides a way of expanding wildcard characters in a non-specific file name into a set of specific filenames. A wildcard is a symbol that can be replaced by one or more characters when the expression is evaluated. The most common wildcard symbols are the question mark (?) which stands for a single character and the asterisk (*) which stands for a contiguous string of characters. In the above example, the backslash disables the implicit replacement of “mydata” by “*mydata*” so you end up with only results containing “mydata.”
The mlocate command is a new implementation of locate. It indexes the entire file system, but the search results only include files to which the current user has access. When you update the mlocate database, it keeps timestamp information in the database. This allows mlocate to know if the contents of a directory changed without reading the contents again and makes updates to the database faster and less demanding on your hard drive.
When you install mlocate, the /usr/bin/locate binary file changes to point to mlocate. To install mlocate, if it’s not already included in your Linux distribution, type the following command at the prompt.
sudo apt-get install mlocate
NOTE: We will show you a command later in this article that allows you to determine where the executable for a command is located, if it exists.
The mlocate command does not use the same database file as the standard locate command. Therefore, you may want to create the database manually by typing the following command at the prompt.
The mlocate command will not work until the database is created either manually or when the script is run from cron.
For more information about either the locate or the mlocate command, type “man locate” or “man mlocate” (without the quotes) at the command prompt in a Terminal window and press Enter. The same help screen displays for both commands.
Using the Which Command
The “which” command returns the absolute path of the executable that is called when a command is issued. This is useful in finding the location of an executable for creating a shortcut to the program on the desktop, on a panel, or other place in the desktop manager. For example, typing the command “which firefox” (without the quotes) displays the results shown in the image below.
NOTE: By default, the which command only displays the first matching executable. To display all matching executables, use the “-a” option with the command. The “which firefox” command would become “which -a firefox.”
You can search for multiple executables using at once, as shown in the following image. Only the paths to executables found are displayed. In the example below, only the “ps” executable was found.
NOTE: The “ps” executable reports a snapshot of the current processes running on your system.
NOTE: The which command only searches the current user’s PATH variable. If you search for an executable that is only available for the root user as a normal user, no results will display.
For more information about the which command, type “man which” (without the quotes) at the command prompt in a Terminal window and press Enter.
Using the Whereis Command
The whereis command is used to find out where the binary, source, and man page files for a command are located. For example, typing “whereis firefox” (without the quotes) at the prompt displays results as shown in the following image.
If you want only the path to the executable to display, and not the paths to the source and the man(ual) pages, use the “-b” option. For example, the command “whereis -b firefox” will display only “/usr/bin/firefox” as the result. This is handy because you will most likely search for a program’s executable file more often than you would search for source and man pages for that program. You can also search for only the source files (“-s”) or for only the man pages (“-m”).
For more information about the whereis command, type “man whereis” (without the quotes) at the command prompt in a Terminal window and press Enter.
Understanding the Difference Between the Whereis Command and the Which Command
The whereis command shows you the location for the binary, source, and man pages for a command, whereas the which command only shows you the location of the binary for the command.
The whereis command searches through a list of specific directories for the binary, source, and man files whereas the which command searches the directories listed in the current user’s PATH environment variable. For the whereis command, the list of specific directories can be found in the FILES section of the man pages for the command.
When it comes to results displayed by default, the whereis command displays everything it finds whereas the which command only displays the first executable it finds. You can change that using the “-a” option, discussed earlier, for the which command.
Because the whereis command only uses paths hard-coded into the command, you may not always find what you are looking for. If you are searching for a program you think might be installed in a directory not listed in the man pages for the whereis command, you might want to use the which command with the “-a” option to find all occurrences of the command throughout the system.