How to check if a command is available in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a command is available in Linux and where it is located. You will explore three different methods: using the which command to find the executable path, using command -v to verify presence and identify built-in commands or aliases, and using the type command to check the command's type.

By completing these steps, you will gain practical skills in identifying and locating commands within your Linux environment, which is essential for effective command-line usage and troubleshooting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/FileandDirectoryManagementGroup -.-> linux/which("Command Locating") subgraph Lab Skills linux/echo -.-> lab-558701{{"How to check if a command is available in Linux"}} linux/ls -.-> lab-558701{{"How to check if a command is available in Linux"}} linux/which -.-> lab-558701{{"How to check if a command is available in Linux"}} end

Locate command path with which

In this step, you will learn how to find the location of a command on your system using the which command. When you type a command in the terminal, the system needs to know where the executable file for that command is located. The which command helps you find this path.

Let's find the path for the echo command that you used in the previous lab.

Type the following command in your terminal and press Enter:

which echo

You should see output similar to this:

/usr/bin/echo

This output tells you that the echo executable is located in the /usr/bin/ directory. /usr/bin is a common directory for storing executable programs that are available to all users.

Now, let's try finding the path for the htop command you installed.

Type the following command and press Enter:

which htop

The output should be similar to:

/usr/bin/htop

This confirms that htop is also located in the /usr/bin/ directory.

The which command is very useful when you need to know exactly which version of a command is being executed, especially if you have multiple versions installed or if the command is located in a less common directory.

Verify command presence with command -v

In this step, you will learn another way to check for the existence and location of a command using command -v. This is similar to which, but it can also tell you if a command is a built-in shell command or an alias, not just an executable file.

Let's use command -v to check for the echo command.

Type the following command in your terminal and press Enter:

command -v echo

You should see output similar to this:

echo

In this case, command -v tells us that echo is a built-in shell command. Built-in commands are part of the shell itself, rather than separate executable files.

Now, let's check for the htop command using command -v.

Type the following command and press Enter:

command -v htop

The output should be similar to:

/usr/bin/htop

Here, command -v gives us the full path to the htop executable, just like which did. This indicates that htop is an external command (an executable file), not a shell built-in.

Using command -v is a robust way to determine how the shell will interpret a command name. It's particularly useful for distinguishing between aliases, built-ins, and external executables.

Check command type using type command

In this step, you will use the type command to get a description of how a command name is interpreted by the shell. The type command is even more detailed than command -v and can tell you if a command is an alias, a keyword, a function, a built-in, or a file.

Let's use type to check the echo command.

Type the following command in your terminal and press Enter:

type echo

You should see output similar to this:

echo is a shell builtin

This output clearly states that echo is a shell built-in command.

Now, let's check the htop command using type.

Type the following command and press Enter:

type htop

The output should be similar to:

htop is /usr/bin/htop

Here, type tells us that htop is located at /usr/bin/htop, indicating it's an executable file.

Let's try one more example with a command that might be an alias. Aliases are shortcuts for longer commands. While there might not be a default alias set up in this environment, let's see what type says about a common command like ls.

Type the following command and press Enter:

type ls

The output might vary depending on the shell configuration, but it will likely show something like:

ls is an alias for ls --color=auto

This indicates that ls is an alias that automatically adds color to the output.

The type command is a powerful tool for understanding how your shell resolves command names and can be very helpful for debugging scripts or understanding command behavior.

Summary

In this lab, you learned how to check if a command is available in Linux and determine its type and location. You first used the which command to find the full path of executable commands like echo and htop, demonstrating how to locate where a command's binary file resides on the system, typically in directories like /usr/bin.

Subsequently, you explored the command -v utility, which provides a more comprehensive check. This command not only indicates the location of executable files but also identifies if a command is a built-in shell command, as shown with the echo command. This distinction is important for understanding how the shell processes different types of commands.