Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Use the Which Command in Linux

Learn how to use the which command in linux with clear steps, practical context, and useful troubleshooting guidance.

Table of Contents

This updated guide examines How to Use the Which Command in Linux and organizes the essential facts, background, and practical takeaways in clear American English.

Binary and path

When you try to run a program or command from a Terminal window, the shell (usually Bash on modern distributions) has to find that command and launch it. A number of commands, such ascd,historyandpwd, are integrated into the shell, so Bash does not take much effort to find them.

But how does Bash locate external commands, programs and other independent binaries? Bash uses paths, exactly a set of paths, each leading to a directory. It then searches each directory to come up with an executable file that matches the command or program you're trying to run. When you find a suitable file, Bash will launch it and give up searching.

You can useechoto check the $ PATH environment variable and see the directories in the path. To do so, enter the following, then pressEnter:

echo $PATH

How to Use the Which Command in Linux — contextual image 1

The output list separates each path with a colon (:. On the computer the article uses, Bash will search the directories in the following order:

  • / usr / local / sbin
  • / usr / local / bin
  • / usr / sbin
  • / usr / bin
  • / sbin
  • / bin
  • / user / games
  • / usr / local / games
  • / snap / bin

There are many directories called / sbin and / bin in the file system, which can lead to some confusion.

Suppose there is an updated version of a program called htg. It has a name in the current directory and you can run it by typing the following command:

./htg

How to Use the Which Command in Linux — contextual image 2

This is not really a program, it just prints the version number and then closes. The new version is 1.2.138.

To run a program in the current working directory, you must enter './' before the program name, so Bash knows where to find it.

Because the article wants to run this particular program from any directory, the example will move the executable program to the / usr / bin directory. Bash will find that program in the path and run it.

For example, there is no need to execute in the current directory, so there is no need to type './' before the program name, as shown below:

sudo mv htg /usr/bin

How to Use the Which Command in Linux — contextual image 3

Now, let the program run by typing:

htg

How to Use the Which Command in Linux — contextual image 4

The command runs, but not the new, updated program, but instead, the older version, 1.2.105.

whichcommand

The problem the article demonstrated above is why thewhichcommand was created.

In this example, we will usewhichand pass the name of the program we are testing as a command line parameter:

which htg

How to Use the Which Command in Linux — contextual image 5

Thewhichcommand reports it found a version of htg in the / usr / local / bin directory. Because that location appears in the path before the directory where we moved the updated htg , Bash will use the previous version of the program.

However, if we use the -a (all) option as shown below, it will continue searching even if a match is found:

which -a htg

How to Use the Which Command in Linux — contextual image 6

It then lists all matching results of any directory in the path.

Therefore, there is a problem, which is that the previous version of the program in the directory is also included in the patch. And that directory is being searched before the directory where we have placed the new version of the program.

To verify, you can enter the following and run each version of the program:

/usr/local/bin/htg
/usr/bin/htg

How to Use the Which Command in Linux — contextual image 7

This explains the cause of the problem, and the solution is very simple.

Actually, you have the options: Delete the old version in / use / local / bin directory or move it from / usr / bin to / usr / local / bin.

See results

Two results do not mean two binary files.

Let's look at an example in which the article will use thewhichcommand with the -a (all) option and find the versions of thelessprogram:

which -a less

How to Use the Which Command in Linux — contextual image 8

Thewhichstatement reports two locations containing a version of thelessprogram, but is that true? It's strange that there are two different versions (or the same version in multiple locations) of thelessprogram installed on Linux computers. Therefore, we will not accept the output fromwhich. Instead, dig a little deeper.

You can use thels,-l(long listing) and-h(human-readable) options to see what happens next:

ls -lh /usr/bin/less

How to Use the Which Command in Linux — contextual image 9

The reported file size is 9 bytes! This is definitely not a full copy ofless.

The first character of the list is an l . A normal file will have hyphens ( - ) as the first character. 'l' is the symbol of the symbolic link. If you miss that detail, the -> icon also indicates this is a symbolic link (you can think of it as a kind of shortcut). This indicates a copy oflessin / bin.

Please try again with thelessversion in / bin:

ls -lh /bin/less

How to Use the Which Command in Linux — contextual image 10

This entry is obviously a real binary executable. The first character of the list is a hyphen ( - ), which means it is a regular file and the file size is 167KB. Therefore, only alesscopy is installed, but there is a symbolic link to it from another directory, which Bash also finds when searching for the path.

Check multiple commands at the same time

You can pass multiple programs and commands towhichand it will check them in order.

For example, if you type:

which ping cat uptime date head

How to Use the Which Command in Linux — contextual image 11

whichcommand will work according to the list of programs and commands you have provided, and then list the results for each program.

In addition to digging into the Linux file system out of curiosity,whichmost useful when you expect a set of behaviors from a command or program, but get something else.

You can usewhichof these cases to verify that the Bash command is starting is the command you want to use.

FAQ

What is How to Use the Which Command in Linux about?

It provides a structured overview of linux, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.