Linux which Command: Command Locating

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you'll embark on a journey as a junior developer at TechCorp, tasked with setting up a new development environment. Your mission is to master the which command, a crucial tool for locating executable files in the system's PATH. This skill will prove invaluable as you navigate the complex world of software development and system administration.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") subgraph Lab Skills linux/echo -.-> lab-215210{{"`Linux which Command: Command Locating`"}} linux/which -.-> lab-215210{{"`Linux which Command: Command Locating`"}} end

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. The echo command: This is used to display a line of text/string on the terminal.
  2. The > operator: This is used to redirect output to a file.

Let's create a script to check for techbuild:

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

This command does a few things:

  • It uses echo to write a shell script.
  • The script content is enclosed in single quotes.
  • The > operator redirects the output of echo to a new file named check_techbuild.sh in your home directory.

Now, let's make this script executable and run it:

chmod +x ~/check_techbuild.sh
~/check_techbuild.sh

This script will print "techbuild is not installed" if the command isn't found.

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.

Now, let's find all instances of the ls command:

which -a ls

You might see:

/usr/bin/ls
/bin/ls

Even though /bin/ls is listed last, it might be the one that's executed when you type ls, depending on your PATH configuration. This demonstrates why understanding PATH priority is crucial in a development environment.

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:

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

This command creates a new file named hello in your home directory (~ is a shortcut for the home directory) and makes it executable. The script simply prints "Hello from TechCorp!" when run.

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. The export command makes the modified PATH available to any subprocesses.

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 been introduced to new concepts like the echo command and output redirection with >.

Additional which command options not covered in this lab include:

  • -i: Ignore case distinctions in both the command name and the PATH search
  • -p: Use the default PATH instead of the current one
  • -v: Verbose mode, prints an error message if the command is not found

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

Other Linux Tutorials you may like