Linux which Command: Command Locating

LinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the which command to find the executable file that a shell will run for a command name. You will check installed commands, handle a command that is not available, list multiple matches, and see how the PATH variable affects the result.

To make the behavior of which easier to understand, this lab also uses a few supporting shell concepts such as simple scripts, executable permissions, and temporary PATH changes. Each new concept is explained in place so you can focus on learning why which returns a specific path.

Understanding the Basics of 'which'

As you settle into your new role at TechCorp, your team lead asks you to verify the installation of some key development tools. The which command will be your go-to utility for this task.

The which command is used to locate the executable file associated with a given command by searching through the directories listed in the PATH environment variable.

Let's start by locating the gcc compiler:

which gcc

You should see output similar to this:

/usr/bin/gcc

This output tells you that the gcc executable is located in the /usr/bin directory. This means when you type gcc in the terminal, the system will execute the file located at /usr/bin/gcc.

Now, let's try to locate another essential tool, python:

which python

You might see something like:

/usr/local/bin/python

The which command searches through the directories listed in your PATH environment variable and returns the first match it finds. If you see a different path, don't worry - it just means Python is installed in a different location on your system.

Handling Non-existent Commands

Your team lead mentions that TechCorp used to use a custom build tool called techbuild. Let's check if it's still installed:

which techbuild

You might not see any output (Bash Shell), or you might see an error message like techbuild not found (Zsh Shell).

This is because which doesn't return anything if it can't find the command in your PATH. Don't be alarmed by the lack of output - it's the command's way of telling you that techbuild isn't found in any of the directories in your PATH.

To make this behavior more explicit, we can use a simple shell script. We'll introduce two new concepts here:

  1. A shell script is a text file that stores commands and runs them in order.
  2. The cat <<'EOF' ... EOF pattern writes multiple lines into a file without making you manage several nested quotes.

Let's create a script to check for techbuild:

cat > ~/check_techbuild.sh <<'EOF'
#!/bin/bash
if which techbuild >/dev/null 2>&1; then
  echo "techbuild is installed"
else
  echo "techbuild is not installed"
fi
EOF

This command creates a file named ~/check_techbuild.sh and fills it with a small Bash script.

Here is what the important lines mean:

  • #!/bin/bash tells Linux to run the script with Bash.
  • which techbuild checks whether the command can be found in your PATH.
  • >/dev/null 2>&1 hides normal output and error output so the script can print its own clear result message.

Now give the file execute permission and run it:

chmod +x ~/check_techbuild.sh

The chmod +x command adds execute permission, which allows Linux to run the file as a command.

~/check_techbuild.sh

This script prints techbuild is not installed because which could not find that command.

Discovering Multiple Installations

As you continue setting up your environment, you realize that TechCorp uses multiple versions of Python for different projects. The -a option of which can help you discover all installations of a command:

which -a python

You might see output like this:

/usr/local/bin/python
/usr/bin/python
/bin/python

This output indicates that there are multiple Python installations on the system. The -a option tells which to print all matching executables in PATH, not just the first one.

Understanding this is crucial because it shows you all the possible Python executables that could be run, depending on your PATH configuration. The first one listed is the one that will be executed when you type python in the terminal.

Understanding PATH Priority

Your team lead explains that the order of directories in the PATH variable determines which version of a command is used by default. Let's examine your PATH:

echo $PATH

You'll see a colon-separated list of directories. For example:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The order matters: directories listed earlier have higher priority. This means if there are two executables with the same name in different directories, the one in the directory that appears first in PATH will be used.

The lab setup already created a custom ls command in ~/custom_bin. Add that directory to the front of your PATH so it gets checked before the system directories:

export PATH=$HOME/custom_bin:$PATH

Now check which ls will run by default:

which ls

You should see output similar to this:

/home/labex/custom_bin/ls

To see every matching ls command in your PATH, run:

which -a ls

You should see your custom command first, followed by the system versions:

/home/labex/custom_bin/ls
/usr/bin/ls
/bin/ls

This demonstrates PATH priority clearly: which ls returns the first matching executable, while which -a ls shows the full search result list.

Creating a Custom Command

Your team lead suggests creating a simple custom command to demonstrate how which interacts with the PATH. Let's create a script called hello in your home directory:

cat > ~/hello <<'EOF'
#!/bin/bash
echo "Hello from TechCorp!"
EOF
chmod +x ~/hello

This creates a new file named hello in your home directory and makes it executable. The script prints Hello from TechCorp! when it runs.

Now, try to run it:

hello

You'll get a "command not found" error because your home directory isn't in the PATH. The PATH is a list of directories where the shell looks for executable files.

Let's add your home directory to the PATH temporarily:

export PATH="$PATH:$HOME"

This command adds your home directory to the end of the PATH for the current terminal session. The export command makes the updated PATH available to commands you run afterward.

Now you can run hello, and which will find it:

hello
which hello

You should see "Hello from TechCorp!" and the path to your script. This demonstrates how which finds executables in any directory listed in your PATH.

Summary

In this lab, you've learned how to use the which command to locate executable files in your system's PATH. You've discovered how to:

  1. Find the location of installed programs
  2. Handle non-existent commands
  3. Discover multiple installations of the same command
  4. Understand PATH priority
  5. Create and locate custom commands

You've also practiced writing simple shell scripts, making files executable with chmod +x, and adjusting PATH to control which command runs.

These skills will be invaluable as you continue your journey as a developer at TechCorp and beyond.