• ARTICLES
SEARCH

How-To Geek

How to Find Files and Folders in Linux Using the Command Line

00_different_commands_for_finding_items

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.

01_search_in_nautilus

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.

find .

The dot after “find” indicates the current directory.

02_find_dot_command

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.

03_find_using_name_argument

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.

sudo updatedb

Enter your password when prompted.

04_updating_locate_database

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.

locate mydata

For example, the above command found two files containing “mydata” and one file containing “data.”

05_using_basic_locate_command

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.”

06_locate_with_b_option

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.

07_installing_mlocate

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.

sudo /etc/cron.daily/mlocate

The mlocate command will not work until the database is created either manually or when the script is run from cron.

08_creating_the_database

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.”

09_using_which_command

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.

10_using_which_command_multiple_programs

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.

11_using_whereis_command

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.

Lori Kaufman is a freelance technical writer who likes to write geeky how-to articles to help make people's lives easier through the use of technology. She loves watching and reading mysteries and is an avid Doctor Who fan.

  • Published 04/30/12

Comments (3)

  1. Erik

    An alternative to the “which” command is to use “command -v”. “command” happens to be POSIX-compliant, which could make a difference if you are using it in a shell script.

  2. Kit Lueder

    Thanks. grep is also really useful, such as to search for files that contain a particular string.

  3. Bob Wartell

    Thanks. Very helpful to me. This article is being saved in my Evernote.

Enter Your Email Here to Get Access for Free:

Go check your email!