How to locate executables not in the PATH

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial will guide you through understanding the Linux PATH variable, locating and running executables outside the PATH, and configuring and troubleshooting the PATH variable. By the end, you'll have a better grasp of how to effectively manage and run executable files on your Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") linux/FileandDirectoryManagementGroup -.-> linux/whereis("`File/Command Finding`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") linux/UserandGroupManagementGroup -.-> linux/unset("`Variable Unsetting`") subgraph Lab Skills linux/env -.-> lab-417379{{"`How to locate executables not in the PATH`"}} linux/which -.-> lab-417379{{"`How to locate executables not in the PATH`"}} linux/whereis -.-> lab-417379{{"`How to locate executables not in the PATH`"}} linux/set -.-> lab-417379{{"`How to locate executables not in the PATH`"}} linux/export -.-> lab-417379{{"`How to locate executables not in the PATH`"}} linux/unset -.-> lab-417379{{"`How to locate executables not in the PATH`"}} end

Understanding the Linux PATH Variable

The PATH variable in Linux is an environment variable that specifies the directories in which the shell should search for executable files. When you run a command in the terminal, the shell looks through the directories specified in the PATH variable to find the executable file that corresponds to the command.

The PATH variable is a colon-separated list of directories. For example, the default PATH variable on Ubuntu 22.04 might look like this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

This means that when you run a command, the shell will search for the executable file in the following directories, in order:

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

The PATH variable is an important part of the Linux system, as it allows you to run executable files without having to specify the full path to the file. For example, if you have a script called my_script.sh in the /home/user/scripts directory, you can run it by typing my_script.sh in the terminal, as long as the /home/user/scripts directory is included in the PATH variable.

Here's an example of how to view and modify the PATH variable in Ubuntu 22.04:

## View the current PATH variable
echo $PATH

## Add a new directory to the PATH variable
export PATH=$PATH:/home/user/scripts

By understanding the Linux PATH variable and how it works, you can more effectively manage and run executable files on your system.

Locating and Running Executables Outside the PATH

While the PATH variable is a convenient way to run executable files, there may be times when you need to run an executable that is not located in one of the directories specified in the PATH. In such cases, you can use the which and whereis commands to locate the executable, and then run it using the full path.

The which command searches for the executable file in the directories specified in the PATH variable and returns the full path to the file if it is found. For example, to find the location of the ls command, you can run:

which ls

This will output the full path to the ls executable, which is typically /usr/bin/ls.

The whereis command is similar to which, but it searches a wider range of directories for the executable file, including system directories and manual pages. For example, to find the location of the gcc compiler, you can run:

whereis gcc

This will output the full path to the gcc executable, as well as the locations of any related files, such as manual pages.

Once you have located the executable file, you can run it using the full path. For example, to run the ls command from the /usr/bin directory, you can use the following command:

/usr/bin/ls

This will run the ls command just like you would if you had run it without the full path, but it allows you to run the executable even if it is not located in one of the directories specified in the PATH variable.

By understanding how to locate and run executables outside the PATH, you can more effectively manage and use the various tools and utilities available on your Linux system.

Configuring and Troubleshooting the PATH Variable

Configuring the PATH variable is an important task for Linux users and administrators. You may need to add new directories to the PATH, remove directories, or troubleshoot issues when commands are not found.

To add a new directory to the PATH, you can use the export command. For example, to add the /home/user/scripts directory to the PATH:

export PATH=$PATH:/home/user/scripts

This will temporarily add the directory to the current session's PATH. To make the change permanent, you can add the export command to your shell's startup file, such as ~/.bashrc or ~/.bash_profile.

To remove a directory from the PATH, you can use the export command with the -d option. For example, to remove the /usr/local/games directory from the PATH:

export PATH=$(echo $PATH | tr ":" "\n" | grep -v "/usr/local/games" | tr "\n" ":")

This command uses a combination of shell tools to remove the specified directory from the PATH.

If you encounter a "command not found" error when trying to run a command, it may be due to an issue with the PATH variable. You can use the which and whereis commands to troubleshoot the issue. For example:

which my_command

If the which command does not return a path, it means the command is not in the PATH. You can then use the whereis command to search for the command:

whereis my_command

If the whereis command returns a path, you can try running the command using the full path:

/path/to/my_command

By understanding how to configure and troubleshoot the PATH variable, you can more effectively manage and use the various tools and utilities available on your Linux system.

Summary

The PATH variable is a crucial part of the Linux system, allowing you to run executable files without specifying their full path. However, there may be times when you need to run an executable that is not located in one of the directories specified in the PATH. In such cases, you can use the "which" and "whereis" commands to locate the executable, and then run it using the full path. By understanding the PATH variable and how to work with executables outside of it, you'll be able to more efficiently manage and run programs on your Linux system.

Other Linux Tutorials you may like