Quick Links

Linux offers six different ways to search, and each has its merits. We'll demonstrate how to use find, locate, which, whereis, whatis, and apropos. Each excels at different tasks; here's how to choose the right tool for the job.

Related: 10 Basic Linux Commands for Beginners

You're spoiled for choice when it comes to commands for searching and finding in Linux. Why so many? Well, they each have their specialties and perform better than the others in certain circumstances. You could think of them as a sort of Swiss-Army knife for searching. We're going to look at each blade in turn and find out its particular strengths.

The find Command

The behavior of the find command is difficult to determine by trial and error. Once you understand the syntax, you start to appreciate its flexibility and power.

The simplest way to use find is to just type find and hit enter.

find

find command in a terminal window

Used in this way find behaves like ls, but it lists all of the files in the current directory and those in subdirectories.

output from find command in a terminal window

Some implementations of find require you to put the . for the current directory. If this is the case with your version of Linux,  use the following command:

find .

find . command in a terminal window

To have find search from the root folder you'd use this command:

find /

fin d/ command in a terminal window

To start the search from your home folder use this command:

find ~

find ~ in a terminal window

Using find With File Patterns

For find to be something more than an auto-recursing version of ls, we must provide it with something to search for. We can provide filenames or file patterns. Patterns make use of wildcards where * means any string of characters and ? means any single character.

Patterns must be quoted to work correctly. It is easy to forget to do this, but if you don't quote the wildcard pattern find won't be able to properly carry out the command you gave it.

With this command, we're going to search in the current folder for files that match the pattern "*.*s". This means any filename that has a file extension that ends in "s". We use the -name option to tell find we're either passing in a filename or a filename pattern.

find . -name "*.*s"

find . -name "*.*s" in a terminal window

find returns these matching files.

Note that two of the file extensions are two characters long and one is three characters long. This is because we used the pattern "*.*s". If we'd only wanted the two character file extensions, we would have used "*.?s".

output of the file command in a terminal window

If we'd known in advance that we were looking for JavaScript ".js" files we could have been more specific in our file pattern. Also, note that you can use single quote marks to wrap the pattern if you prefer.

find . -name '*.js'

find . -name "*.js" in a terminal window

This time find only reports on the JavaScript files.

JavaScript files found by find in a terminal window

Ignoring Case With find

If you know the name of the file you want find to locate, you can pass that to find instead of a pattern. You don't need to wrap the filename in quotes if there are no wildcards in it, but it is good practice to do it all the time. Doing so means you won't forget to use them when do you need them.

find . -name 'Yelp.js'

find . -name 'Yelp.js' in a terminal window

That didn't return anything. But's odd, we know that file must be there. Let's try again and tell find to ignore case. We do that by using the -iname option (ignore case name)

find. -iname 'Yelp.js'

fin d. -iname 'Yelp.js' in a terminal window

That was the problem, the filename starts with a lowercase "y", and we were searching with an uppercase "Y."

Recursing Subdirectories with find

One great thing about find is the way it recursively searches through subdirectories. Let's search for any files that start with "map."

find . -name "map*.*"

find . -name "map*.*" in a terminal window

The matching files are listed. Note that they are all in a subdirectory.

Results from a subdirectory in a terminal window

Searching for Directories With find

The -path option makes find look for directories. Let's look for a directory that we can't quite remember the name of, but we know it ends with the letters "about."

find . -path '*about'

find . -path '*about' in a terminal window

The directory is found, it is just called "about," and it is nested inside another directory within the current directory.

Found directory in a terminal window

There is an -ipath (ignore case path) option that allows you to search for paths and to ignore case, similar to the -iname option discussed above.

Using File Attributes with find

find can look for files that have attributes that match the search clue. For example, you can look for files that are empty using the -empty option, regardless of what they're called.

find . -empty

find . -empty in a terminal window

Any zero byte length files will be listed in the search results.

empty file search results in a terminal window

The -executable option will find any file that can be executed, such as a program or a script.

find . -executable

find . -executable in a terminal window

The results list a file called "fix_aptget.sh".

They also contain three directories, including '.', the current directory. The directories are included in the results because the execute bit is set in their file permissions. Without this, you wouldn't be able to change into ("run") those directories.

executable file search results na terminal window

The -type Option

The -type option allows you to search for the type of object you are looking for. We're going provide the type indicator "f" as a parameter to the -type option because we want find to search for files only.

find . executable -type f

find . executable -type f in a terminal window

This time the subdirectories are not listed. The executable script file is the only item in the results.

search results with no directories in a terminal window

We can also ask find to only include directories in the results. To list all the directories, we can use the -type option with the type indicator "d".

find . type -d

find . type -d in a terminal window

Only directories and subdirectories are listed in the results.

directories listed in a terminal window

Using Other Commands With find

You can perform some additional action on the files that are found. You can have the files passed, in turn, to some other command.

If we need to make sure there are no executable files in the current directory and subdirectories, we could use the following command:

find . -name "fix_aptget.sh" -exec chmod -x '{}' \;

find . -name "fix_aptget.sh" -exec chmod -x '{}' \; in a terminal window

The command means:

  • Search in the current directory for a named object called "fix_aptget.sh".
  • If it is found execute the chmod command.
  • The parameters that are passed to chmod are -x to remove executable permissions and '{}' which represents the filename of the found file.
  • The final semicolon marks the end of the parameters that are going to be passed to chmod. This has to be 'escaped' by preceding it with a '\' backslash.

Once this command has been run, we can search for executable files as before, and this time there will be no files listed.

Search results with no executable files ina terminal window

To cast our net wider, we could use a file pattern instead of the filename we used in our example.

This flexibility allows you to search for specified file types, or with filename patterns, and have some action carried out on the matching files.

Find has many other options, including searching for files by their modified date, files owned by a user or group, files that are readable, or files that have a specific set of file permissions.

The locate And mlocate Commands

Many Linux distributions used to have a copy of locate included with them. This was superseded by the mlocate command, which was an improved and updated version of locate.

When mlocate is installed on a system it modifies the locate command so that you actually use mlocate even if you type locate.

Current versions of Ubuntu, Fedora, and Manjaro were checked to see whether they had versions of these commands pre-installed on them. Ubuntu and Fedora both included mlocate. It had to be installed on Manjaro, with this command:

sudo pacman -Syu mlocate

sudo pacman -Syu mlocate in a terminal window

On Ubuntu, you can use locate and mlocate interchangeably. On Fedora and Manjaro you must type locate , but the command is executed for you by mlocate.

If you use the  --version option with locate you'll see that the command that responds is actually mlocate.

locate --version

locate --version in a terminal window

Because locate works on all of the Linux distributions that were tested, we'll use locate in our explanations below. And it's one less letter to type.

The locate Database

The biggest advantage that locate has is speed.

When you use the find command, it dashes off and performs a search across your filesystem. The locate command works very differently. It does a database lookup to determine whether what you are looking for is on your computer. That makes the search much faster.

Of course, it does raise an obvious question about the database. What ensures the database is up to date? When mlocate is installed it (usually) places an entry in cron.daily. This runs each day (very early in the morning) and updates the database.

To check whether this entry exists, use this command:

ls /etc/cron.daily/*loc*

ls /etc/cron.daily/*loc* in a terminal window

If you don't find an entry there, you could set up an automated task to do this for you at the time you choose.

Related: How to Schedule Tasks on Linux: An Introduction to Crontab Files

What if your computer isn't on at the time when the database is supposed to be updated? You can manually run the database update process with the following command:

sudo updatedb

sudo updatedb in a terminal window

Using locate

Let's look for files that contain the string "getlatlong". With locate, the search automatically looks for any matches that contain the search term anywhere in the filename, so there is no need to use wildcards.

locate getlatlong

locate getlatlong in a terminal window

It's hard to convey speed in a screenshot, but almost immediately the matching files are listed for us.

locate results with files containing getlatlong in a terminal window

Telling locate How Many Results You Want

Sometimes you may know there are lots of files of the type your searching for. You only need to see the first few of them. Perhaps you just want to be reminded which directory they are in, and you don't need to see all of the filenames.

Using the -n (number) option you can limit the number of results that locate will return to you. In this command, we've set a limit of 10 results.

locate .html -n 10

locate .html -n 10 in a terminal window

locate responds by listing the first 10 matching file names it retrieves from the database.

search results from located limited to 10 results in a terminal window

Counting Matching Files

If you only want to know the number of matching files and you don't need to know what they are called or where they are on your hard drive, use the -c (count) option.

locate -c .html

locate -c .html in a terminal window

So, now we know there are 431 files with the ".html" extension on this computer. Maybe we do want to have a look at them, but we thought we'd take a peek and see how many there were first. Armed with that knowledge we know we'll need to pipe the output through less.

locate .html | less

sudo pacman -Syu mlocate in a terminal window

And here they all are, or at least, here's the top of the long list of them.

sudo pacman -Syu mlocate in a terminal window

Ignoring Case With locate

The -i (ignore case) causes locate to do just that, it ignores uppercase and lowercase differences between the search term and the filenames in the database. If we try and count the HTML files again, but mistakenly provide the search term in uppercase we'll get zero results.

locate -c .HTML

sudo pacman -Syu mlocate in a terminal window

By including the -i option we can make locate ignore the difference in case, and return our expected answer for this machine, which is 431.

locate -c -i .HTML

sudo pacman -Syu mlocate in a terminal window

The locate Database Status

To see the status of the database, use the -s (status) option. This causes locate to return some statistics about the size and contents of the database.

locate -s

sudo pacman -Syu mlocate in a terminal window

The which Command

The which command searches through the directories in your path, and tries to locate the command you are searching for. It allows you to determine which version of a program or command will run when you type its name on the command line.

Imagine we had a program called geoloc. We know it is installed on the computer, but we don't know where it is located. It must be in the path somewhere because when we type its name, it runs.  We can use which to locate it with this command:

which geoloc

which geoloc in a terminal window

which reports that the program is located in /usr/local/bin.

geoloc in /usr/local/bin

We can check whether there are any other copies of the program in other locations within the path by using the -a (all) option.

which -a geoloc

which -a geoloc in a terminal window

This shows us that we have the geoloc program in two places.

which -a geoloc in a terminal window

Of course, the copy in /usr/local/bin is going to be found first by the Bash shell every time, so having the program in two places is meaningless.

Removing the version in /usr/bin/geoloc will save you a bit of hard drive capacity. More importantly, it will also avoid issues created by someone manually updating the program, and doing it in the wrong place. Then wondering why they don't see the new updates when they run the program.

The whereis Command

The whereis command is similar to the which command, but it is more informative.

In addition to the location of the command or program file, whereis also reports where the man (manual) pages and source code files are located. In most cases, the source code files won't be on your computer, but if they are, whereis will report on them.

The binary executable, the man pages and the source code are often referred to as the "package" for that command. If you want to know where the various components of the package for the diff command are located, use the following command:

whereis diff

whereis diff in a terminal window

whereis responds by listing the location of the diff man pages and the diff binary file.

whereis resuts for diff in a terminal window

To restrict the results to only show the location of the binary (in effect, make whereis work like which ) use the -b (binary) option.

whereis -b diff

whereis -b diff in a terminal window

whereis only reports on the location of the executable file.

whereis output restricted to binary location only in a terminal window

To restrict the search to report only on the man pages use the -m (manual) option. To restrict the search to report only on the source code files use the -s (source) option.

To see the locations that whereis searches through, use the -l (locations) option.

whereis -l

whereis -l in a terminal window

The locations are listed for you.

whereis search locations listed in a terminal window

Now that we know the locations whereis will search in, we can, should we choose, restrict the search to a particular location or group of locations.

The -B (binary list) option restricts the search for executable files to the list of paths provided on the command line. You must provide at least one location for whereis to search through. The -f (file) option is used to signal the end of the location last the start of the filename.

whereis -B /bin/ -f chmod

whereis -B /bin/ -f chmod in a terminal window

whereis looks in the single place we asked to search through. That happens to be where the file is located.

whereis results from using the -B option in a terminal window

You can also use the -M (manual list) option to restrict searches for man pages to the paths you provide on the command line. The -S (source list) option allows you to restrict the search for source code files in the same way.

The whatis Command

The whatis command is used to quickly search through the man (manual) pages. It provides one-line summary descriptions of the term you've asked it to search for.

Let's start with a simple example. Although it looks like the starting point of deep philosophical debate, we're just asking whatis to tell us what the term "man" means.

whatis man

whatis man in a terminal window

whatis finds two matching descriptions. It prints a short description for each match. It also lists the numbered section of the manual that contains each full description.

whatis results in a terminal window

To open the manual at the section that describes the man command, use the following command:

man 1 man

man 1 man in a terminal window

The manual opens at section man(1), at the page for man.

man page open at section one in a terminal window

To open the manual at section 7, at the page that discusses the macros you can use to generate man pages, use this command:

man 7 man

man 7 man in a terminal window

The man page for the man macros is displayed for you.

man page open at section seven in a terminal window

Searching In Specific Sections of the Manual

The -s (section) option is used to limit the search to sections of the manual you are interested in. To have the whatis search restricted to section 7 of the manual, use the following command. Note the quote marks around the section number:

whatis -s "7" man

whatis -s "7" man in a terminal window

The results only refer to section 7 of the manual.

whatis results restricted to section seven in a terminal window

Using whatis With Wildcards

You can use wildcards with whatis. You must use the -w (wildcard) option to do so.

whatis -w char*

whatis -w char* in a terminal window

The matching results are listed in the terminal window.

whatis man in a terminal window

The apropos Command

The apropos command is similar to whatis, but it has a few more bells and whistles. It searches through the man page titles and one line descriptions looking for the search term. It lists the matching man page descriptions in the terminal window.

The word apropos means "related to" or "concerning," and the command apropos took its name from this.  To search for anything related to the groups command, we can use this command:

apropos groups

apropos groups in a terminal window

apropos lists the results to the terminal window.

apropos results for group in a terminal window

Using More Than One Search Term

You can use more than one search term on the command line. apropos will search for man pages that contain either of the search terms.

apropos chown chmod

apropos chown chmod in a terminal window

The results are listed as before. In this case, there is a single entry for each of the search terms.

apropos results for chmod and chown in a terminal window.

Using Exact Matches

apropos will return man pages that contain the search term even if the term is in the middle of another word. To make apropos return only exact matches for the search term, use the -e (exact) option.

To illustrate this, we'll use apropos with grep as the search term.

apropos grep

apropos grep in a terminal window

There are many results returned for this, including many where grep is incorporated in another word, such as bzfgrep.

results for apropos grep in a terminal window

Let's try that again and use the -e (exact) option.

apropos -e grep

apropos -e grep in a terminal window

We have a single result this time, for what we were actually searching for.

results for apropos -e grep in a terminal window

Matching All Search Terms

As we saw earlier if you provide more than one search term apropos will search for man pages that contain either search term. We can change that behavior by using the -a (and) option. This makes apropos only select matches that have all of the search times in them.

Let's try the command without the -a option so that we can see what results apropos gives.

apropos crontab cron

apropos crontab cron in a terminal window

The results include man pages that match one or the other of the search terms.

apropos groups in a terminal window

Now we'll use the -a option.

apropos -a crontab cron

apropos groups in a terminal window

This time the results are narrowed down to those containing both search terms.

apropos groups in a terminal window

Yet More Options

All of these commands have more options---some of them many more options---and you are encouraged to read the man pages for the commands we've discussed in this article.

Here's a quick summary for each command:

  • find: Provides a feature rich and granular search capability to look for files and directories.
  • locate: Provides a fast database-driven search for programs and commands.
  • which: Searches the $PATH looking for executable files
  • whereis: Searches the $PATH looking for executable files, man pages, and source code files.
  • whatis: Searches the man one-line descriptions for matches to the search term.
  • apropos: Searches the man page with more fidelity than whatis, for matches to the search term or terms.

Looking for more Linux terminal information? Here are 37 commands you should know.

Related: 37 Important Linux Commands You Should Know