Most people 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 Linux, no matter what desktop manager you use.

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/ 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, run man find  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  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 displays the results shown in the image below.

09_using_which_command

By default, the which command only displays the first matching executable. To display all matching executables, use the -a option with the command:

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.

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

Linux Commands

Files

tar · pv · cat · tac · chmod · grep ·  diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

RELATED: Best Linux Laptops for Developers and Enthusiasts