How to use the what command on Linux

The Linux which command identifies the binary executable that starts when you issue a command to the shell. If you have different versions of the same program on your computer, you can which to find out which one the shell will use.

Binaries and paths

When you try to run a program or command from the Terminal window, the shell (usually Bash on modern distributions) must find this command and run it. Some commands, such as CD, story, and pwd, are built into the shell so Bash doesn’t have to work too hard to find them.

But how does Bash find other commands, programs, and external stand-alone binaries? Well, bash uses the path, which is actually a collection of paths, each of which points to a directory. It then searches each of those directories for an executable file that corresponds to the command or program you want to run. If it finds one, Bash starts it and stops searching.

You can use echo to check that $PATH Environment variable and look at the directories in your path. To do this, enter the following, then press Enter:

echo $PATH

The output list separates each path with colons (:). On the computer we are using, Bash searches the following directories in this order:

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


There are many folders called /sbin and /bin in the filesystem, which can lead to confusion.

Watch these paths

Let’s say we have an updated version of a program called htg. It’s in our current directory and we can run it by typing the following command:

./htg 

It’s not a big 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 put “./” in front of the program name so Bash knows where to find it.

Since we want to run this particular program from any directory, let’s move the executable into the /usr/bin Directory. Bash finds this program in the path and runs it for us.

We don’t need the executable file in our current directory, nor do we have to enter “./” in front of the program name, as shown below:

sudo mv htg /usr/bin

Now let’s try running the program by typing:

htg


Something’s going on, but it’s not our new, updated program. Rather, it is the older version 1.2.105.

What command

The problem we showed above is why the which command was designed.

In this example, we use which and pass the name of the program we are investigating as the command line parameter:

which htg

which reports that a version of was found htg by doing /usr/local/bin Directory. Because this location appears in the path before the directory where we moved the updated ones htg, Bash uses this earlier version of the program.

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

which -a htg

It then lists all the matches in one of the directories in the path.


So that’s the problem – there is an earlier version of the program in a directory that is also in the patch. And this directory is searched before the directory in which we have stored the new version of the program.

To check, we can type in the following and run each version of the program explicitly:

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

This explains the problem and the solution is simple.

We actually have options. We can either delete the old version im /use/local/bin Directory or move it off /usr/bin to /usr/local/bin.

Look at these results

Two results do not necessarily mean two binaries.

Let’s look at one example in which we’ll use them which Command with the -a (all) option and look for versions of the less Program:

which -a less

which reports two locations that have a version of the less Program, but is that right? It would be strange to have two different versions (or the same version in multiple locations) of the. to have less installed on a Linux computer. So we will not accept the issue of which. Instead, let’s dig a little deeper.

We can use that ls, -l (long listing) and -h Options (human readable) to see what’s going on:

ls -lh /usr/bin/less


The file size is specified with nine bytes! This is definitely not a complete copy of the less.

The first character in the listing is an “l”. A normal file would have a hyphen (-) as the first character. The “l” is a symbol that means symbolic connection. If you missed this detail, --> Icon also indicates that this is a. is symbolic link, which you can think of as a kind of abbreviation. This points to the copy of less in /bin.

Let’s try again with the version of less in /bin:

ls -lh /bin/less

This entry is obviously a “real” binary executable. The first character in the listing is a hyphen (-), which means it is a normal file and the file size is 167 KB. So just a copy of less is installed, but there is a symbolic link to it from another directory that Bash will also find when it searches the path.

How to use the ls command to list files and directories on Linux

Check multiple commands at the same time

You can use multiple programs and commands which, and it will check them in turn.

To the examplewhen you type:

which ping cat uptime date head

which works through the list of programs and commands you gave it, listing the results for each one.

Which is which

If you are so inclined, you can use it too which on yourself by typing:

which which


Besides poking around the Linux filesystem out of curiosity, which is most useful when you expect one behavior from one command or program but get another.

You can use which In these cases, you can check that the command launched by bash is the one you want to use.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts