Linux which Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the which command in Linux to locate the path of executable files on your system. The which command is a useful tool for quickly identifying the location of commands, scripts, and other executable files. You will explore the basic usage of the which command, as well as some advanced techniques for finding executable files, even when they are not in the standard system directories.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") linux/FileandDirectoryManagementGroup -.-> linux/whereis("`File/Command Finding`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/echo -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} linux/test -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} linux/pwd -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} linux/which -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} linux/whereis -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} linux/chmod -.-> lab-423007{{"`Linux which Command with Practical Examples`"}} end

Understand the Purpose of the which Command

In this step, you will learn about the purpose and usage of the which command in Linux. The which command is used to locate the path of executable files in the system.

First, let's try running the which command to find the location of the ls command:

which ls

Example output:

/usr/bin/ls

The output shows that the ls command is located at the /usr/bin/ls path. The which command searches through the directories specified in the PATH environment variable to find the executable file.

You can also use the which command to find the location of other commands, such as python, git, or nano:

which python
which git
which nano

Example output:

/usr/bin/python3
/usr/bin/git
/usr/bin/nano

The which command is useful when you need to know the exact location of an executable file, especially when you have multiple versions of the same command installed on your system.

Locate the Path of Executable Files Using the which Command

In this step, you will learn how to use the which command to locate the path of executable files on your system.

First, let's create a simple script called hello.sh in the ~/project directory:

cd ~/project
echo "#!/bin/bash" > hello.sh
echo "echo 'Hello, World!'" >> hello.sh
chmod +x hello.sh

Now, let's use the which command to find the path of the hello.sh script:

which hello.sh

Example output:

/home/labex/project/hello.sh

The output shows that the hello.sh script is located at the /home/labex/project/hello.sh path.

You can also use the which command to find the path of other executables, such as system commands or installed applications. For example:

which python
which git
which nano

Example output:

/usr/bin/python3
/usr/bin/git
/usr/bin/nano

The which command is a useful tool for quickly identifying the location of executable files on your system.

Explore Advanced Usage of the which Command

In this final step, you will explore some advanced usage scenarios of the which command.

One advanced use case is to use the which command to find the first occurrence of an executable in your PATH. For example, let's say you have multiple versions of Python installed on your system, and you want to find the first one in your PATH:

which python

Example output:

/usr/bin/python3

The which command will return the first instance of the python executable it finds in your PATH.

You can also use the which command with the -a option to list all the occurrences of an executable in your PATH:

which -a python

Example output:

/usr/bin/python3
/usr/bin/python3.10
/usr/bin/python

This can be useful when you have multiple versions of the same command installed and want to see all the available options.

Another advanced usage is to use the which command to check if a command exists in your PATH. You can do this by checking the exit code of the which command:

which non_existent_command
echo $?

Example output:

echo $?
1

If the command is not found in your PATH, the which command will return a non-zero exit code, indicating that the command does not exist.

These advanced techniques can be helpful when working with complex system environments or when you need to troubleshoot issues related to executable file locations.

Summary

In this lab, you learned about the purpose and usage of the which command in Linux. The which command is used to locate the path of executable files in the system by searching through the directories specified in the PATH environment variable. You explored how to use the which command to find the location of various commands, such as ls, python, git, and nano. Additionally, you learned how to use the which command to locate the path of a custom script you created, demonstrating its usefulness in identifying the location of executable files on your system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like