BASH Programming Utilities

Find Path of a Command with whereis

whereis is used to find the path of the Linux binary /executable files, source files and man page files.There are many Linux distributions. Not every Linux distribution keeps the binary/executable files, source files and man page files in the same location. So, to find out the path of these files when needed, the whereis command is used. It is a very useful command for every Linux system administrator out there.

In this article, I am going to show you how to use the whereis command on Linux.  So, let’s get started.

Basic Usage of the whereis Command:

You can find the full path of any standard Linux command that is in the PATH of your Linux operating system.

NOTE: The PATH contains the directories where the standard Linux commands and other commands are installed. You can run the following command to print the PATH on your console.

$ echo $PATH

Let’s say, you want to know the full path of the Linux command passwd. To do that, run the whereis command as follows:

$ whereis passwd

As you can see, the full path of the passwd command is /usr/bin/passwd.

The whereis command without any option prints the full path of the command, the full path of the configuration file that matches the query, and the full path of the matched man page files.

Printing the Full Path of Only Binaries/Executables:

If you only want to print the full path of specific Linux commands and configuration files, then you can do so with the -b option of whereis.

Let’s say, you want to print only the full path of the command ls, then run the whereis command as follows:

$ whereis -b ls

As you can see, the full path of the command ls is /bin/ls.

If there is a matching configuration file, it should be printed as well. For example, when you try to find the full path of the command passwd, you should also get the full path of the passwd configuration file as you can see in the screenshot below.

Printing the Full Path of Only Man Pages:

If you want to print the full path of only man pages on your computer, then you can use the -m option of the whereis command.

For example, let’s say, you want to find out the full path of all man page files of the ls command. To do that, run the whereis command as follows:

$ whereis -m ls

As you can see, the man page of the ls command is in the path /usr/share/man/man1/ls.1.gz.

The same way, you can find the full path of all the man pages of the useradd command.

$ whereis -m useradd

Finding the Full Path of Linux Headers or Kernel Source Files Only:

You can use the -s option of the whereis command to print the full path of the Linux headers or kernel source files used to develop system software.

For example, to find the full path of the source file net, you can use the whereis command as follows:

$ whereis -s net

As you can see, there are two locations where the net source header file is kept on my Ubuntu 18.04 LTS, /usr/src/linux-headers-4.15.0-20-generic/net and /usr/src/linux-headers-4.15.0-20/net.

The same way, you can search for the fs source file with the whereis command as follows.

$ whereis -s fs

Printing the Directories the whereis Command Searched:

By default, the whereis command searches a lot of system directories. You can use the -l option to tell whereis to print the directories it searched.

$ whereis -l useradd

As you can see, the whereis command searched a lot of directories by default.

Output continued.

Adding a New Search Directory or Directories:

You saw earlier that the whereis command searches a lot of directories by default. But if you have some directories that you want whereis to search, then you can use the -B, -M or -S option of the whereis command to specify binary/executable, manual or man pages, source or kernel header directories respectively.

Let’s say, you have some binary files in the ~/bin directory that you want to include in the whereis search. To do that, you can run the whereis command as follows:

$ whereis -B ~/bin -f CreateDirectory

NOTE: Whenever you use either the -B, -M or the -S option, you must also use the -f option as shown above. Otherwise, you will get an error.

As you can see, the full path of the CreateDirectory command is printed. So, it worked. We’ve successfully added a new binary directory to the whereis search path.

You can also add more than one search directories if you want.

For example, let’s say, you want to add the ~/bin and ~/bin2 binary directories to the whereis search path. To do that, run the whereis command as follows:

$ whereis -B ~/bin ~/bin2 -f CreateFile

The same way, you can add manual or man page directories to the whereis search path as follows:

$ whereis -M ~/man ~/doc -f mydoc

The same way, for the source or kernel headers, you can use the -S option to add addition search directories as follows:

$ whereis -S ~/linux-headers-4.19 ~/linux-headers-4.20 -f net

You can also mix the -B, -M and -S options as required to add the required search path for the binary, manual or source files respectively.

$ whereis -B ~/bin ~/bin2 -M ~/doc ~/doc2 -f searchQuery

So, that’s how you use the whereis command to find the path of commands/binaries/executables, man pages or manuals, and source files in Linux. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.