Complete Guide to Add and Remove Directory to PATH Variable in Linux

How to Add and Remove Directory to PATH Variable in Linux

As a Linux user, you might need to execute a command with no path specifying. The PATH is an environment variable and an essential component of the Linux system. In this article, you will learn How to Add and Remove Directory to PATH Variable in Linux. Finding the location of the commands you run is possible by PATH variable in Linux. Directories can be added/removed To PATH temporarily and permanently.

When you run a command in Linux, the PATH variable stores the path to the folders where it should look for executables. If you want to run some executables as commands from anywhere in the system, you should add their location in the PATH variable. So, after buying Linux VPS, you must know how to specify the locations of the binaries of the software in the PATH you install.

Join us with this guide to learn more about the PATH environment variable, how it works, and the way you can use it to add and remove your considered directories. In the end, you are skilled in setting PATH variable in Linux and dealing with PATH in Linux.

What is PATH and How it Works?

PATH is one of the background manipulators on your Linux machine. It subtly influences how you use the site. The system runs a program when users enter a command on the terminal. Linux must therefore be able to find the proper program. The system is told where to look for a program to run by PATH, which specifies program directories. In this manner, Linux checks the $PATH variable to see a list of folders to hunt for the program when a command is entered into the terminal. Because of the $PATH variable, we don’t need to give paths for several commands. It is possible to modify this variable to instruct our Linux system where to hunt for specific apps.

The default shell on the majority of Linux distributions is bash. When you enter a line of text, the program reads it and recognizes the command names mixed in with the parameters, pipes, redirections, and other elements. The executable binaries for those commands are then located, and they are launched using the parameters you provided. Shell builtins come first in the shell’s search for executables in the PATH, followed by a left-to-right scan of the directories listed. Modifying your.bashrc or .profile files, you can use the export command to temporarily or permanently add directories to your PATH variable in Linux.

The most straightforward builtins to locate are those that are part of the shell itself. The PATH environment variable Linux finds what you are looking for. It contains a list of the locations the shell examines as well as the sequence in which those locations will be searched. It won’t be searched if the current working directory isn’t on your path. Additionally, the shell won’t find commands that are saved in directories that aren’t in the path.

View the Directories in PATH

Check PATH variable Linux is one of the actions you will need to do. The PATH consists of several directories (like /usr/local/sbin, /usr/bin, and more). To list your PATH, run the command below:

echo $PATH
<span style="color: #808080;">Output</span>

Linux list PATH variables is shown above. The output is a list of file system locations separated by colons (:). You must view all the configured directories in the system’s PATH variable in your output. The following command prints the same output:

printenv PATH

Also, you can use the command below to view what directory a command belongs to:

which date
<span style="color: #808080;">Output</span>

The output shows that the executable for data is located in the /bin/ directory.

Prerequisites to Add and Remove a Directory to PATH

To let this tutorial work correctly, provide the options below and move on.

  • Access to your Terminal.
  • A Text Editor.

Practical Examples Add Directory to PATH Variable in Linux

Let’s go through this guide and review some examples to learn about Linux path environment variable. While specific directories can be added to PATH by default, you can add other directories to PATH temporarily or permanently.

1. Add a Directory to PATH in Linux Temporarily

Run the following command to temporarily add a directory to the PATH. In this example, you will add the Directory1 to PATH.

export PATH="/Directory1:$PATH"

As we mentioned, you can use the $ echo $PATH command to verify that the directory has been added.

$ echo $PATH
<span style="color: #808080;">Output</span>
/local/games: /snap/bin

This setting is only valid for the current session and the configuration will change when you reboot the PC or close the terminal. Check out the next step to learn how to make it permanent.

2. Add a Directory to PATH in Linux Permanently

As we mentioned, you can add a directory to PATH in Linux permanently to prevent changing the configuration after every reboot. To do this, you need to edit the .bashrc file of the user you want to change. Open the file stored in the home directory using your preferred text editor.

$ nano ~/.bashrc

Put the directory you consider adding to $PATH permanently at the end of this file.

export PATH="/Directory1:$PATH"

Finally, save the changes and exit the file. To make the changes take effect, run :

$ source ~/.bashrc

Also, you can log out or reboot the system to achieve the same purpose.

$ echo $PATH

A directory for the current user only can be added by editing the.bashrc file. Edit the .profile file to add the path to the PATH for all users.

Methods to Remove Directory to PATH Variable in Linux

To remove a directory from the PATH variable in Linux, four different methods will be explained since we cannot introduce any particular command. Let’s continue this part to learn them.

Method 1. Close the Terminal

According to the add a Directory to PATH in Linux temporarily part, you now know that you can easily remove a directory from PATH if it was added temporarily. Because it lasts only for your current session and the directory is removed from PATH automatically when the session ends. So, you can simply exit the terminal or reboot your system to delete a temporary directory from PATH.

Method 2. Edit Configuration Files

By editing the relevant file and deleting the undesired directory, you can remove a directory from PATH. The /.bashrc file (per user) or the /etc/environment can be used to set the folders for PATH. To do this, open the file in your preferred text editor, go to the end of the file, and remove the directory.

While deleting folders from .bashrc only affects one person, doing so on the /etc/environment would have an impact on the entire system.

Method 3. String Replacement

Another method to remove a directory from PATH is to use string replacement. To do this, run the following command to remove the string from your current session:

export PATH=${PATH/'/Directory1'/}

Method 4. Combine Commands

The last method to Remove Directory to PATH Variable in Linux suggests using tr, grep, and paste commands. For example:

export PATH="$( echo $PATH| tr : '\n' |grep -v Directory1 | paste -s -d: )"


In this article, you learned How to Add and Remove Directory to PATH Variable in Linux. Two temporary and permanent ways to add directory and four methods to remove directory were explained. If you know more commands to add or remove directory to PATH variable in Linux, then the comment section is all yours.

Leave a Reply

Your email address will not be published. Required fields are marked.