How to use the which command in Linux

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

Instructions for using which command in Linux

  1. Binary and path
  2. See the links
  3. Which command
  4. See results
  5. Check multiple commands at the same time

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 as cd , history and pwd , 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 use echo to check the $ PATH environment variable and see the directories in the path. To do so, enter the following, then press Enter :

 echo $PATH 

How to use the which command in Linux Picture 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:

  1. / usr / local / sbin
  2. / usr / local / bin
  3. / usr / sbin
  4. / usr / bin
  5. / sbin
  6. / bin
  7. / user / games
  8. / usr / local / games
  9. / snap / bin

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

See the links

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 Picture 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 Picture 3

Now, let the program run by typing:

 htg 

How to use the which command in Linux Picture 4

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

which command

The problem the article demonstrated above is why the which command was created.

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

 which htg 

How to use the which command in Linux Picture 5

The which command 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 Picture 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 Picture 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 the which command with the -a (all) option and find the versions of the less program:

 which -a less 

How to use the which command in Linux Picture 8

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

You can use the ls , -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 Picture 9

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

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 of less in / bin.

Please try again with the less version in / bin:

 ls -lh /bin/less 

How to use the which command in Linux Picture 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 a less copy 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 to which and 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 Picture 11

which command 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, which most useful when you expect a set of behaviors from a command or program, but get something else.

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

3.3 ★ | 3 Vote

May be interested

  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • Use the Top command to see the processes running in LinuxUse the Top command to see the processes running in Linux
    the top command in linux is used to display all processes running in the linux environment. this tutorial shows you how to use the top command through explaining the various parameters available and the content they display.
  • How to use the dmesg command in LinuxHow to use the dmesg command in Linux
    the dmesg command has been used to troubleshoot server and linux desktops for decades. it's time to start using this handy command!