Example Uses of the Linux which Command

Find where your Linux applications are installed

The Linux which command is used to find the location of a program. Here's how to use the which command and how to get the most out of it by using the available switches.

How to Find the Location of a Program

In theory, all programs run from the /usr/bin folder, but in reality, this is not the case. The best way to find out where a program is located is to use the which command.

The simplest form of the command is as follows:

which <program name>

For example, to find the location of the Firefox web browser, use the following command:

which firefox

The output is:

/usr/bin/firefox
Linux which command

You can specify multiple programs in the same command. For example:

which firefox gimp banshee

This returns the following results:

/usr/bin/firefox
/usr/bin/gimp
/usr/bin/banshee
Linux which command multiple programs

Some programs are located in more than one folder. By default, however, the which command displays only one.

For example, run the following command:

which less

This finds the location of the less command and the output is as follows:

/usr/bin/less

This doesn't show the whole picture, however, because the less command is available in more than one place.

The which command shows all the places a program is installed using the following switch:

which -a <program name>

You can run this against the less command as follows:

which -a less

The output from the above command is as follows:

/usr/bin/less
/bin/less
Linux which command multiple results

So does that mean less is installed in two places? Actually, no.

Run the following ls command:

ls -lt /usr/bin/less

At the end of the output, you see the following:

/usr/bin/less -> /bin/less

When you see the -> at the end of the ls command, it is a symbolic link, and it points to the location of the real program.

Now, run the following ls command:

ls -lt /bin/less

This time the output at the end of the line is as follows:

/bin/less

This means that this is the real program.

It is may be surprising that the which command outputs /usr/bin/less when you search for the less command.

A command that may be more useful than the which command is the whereis command. The whereis command locates the binaries for the program, the source code for the program, and the manual pages for the program.

Summary

So why would you use the which command?

If a program is installed, but for some reason, it won't run, it may be because the folder the program was installed to isn't in the path. By using the which command, you can locate where the program is. Then, you can either navigate to the folder the program is in to run it or add the path to the program to the path command.

Other Useful Search Tools

There are other commands that are useful for finding files. Use the find command to find files on your file system or use the locate command.

Linux Essential Commands

Modern Linux distributions have made the requirement to use the terminal less of an issue, but there are some commands that you need to know.

Some essential commands help navigate the file system. These commands show which folder you are in, navigate to different folders, list the files in folders, get back to your home folder, create a new folder, create files, rename and move files, and copy files.