For anyone working with command-line tools and scripts, managing the system’s PATH (short for “Path to All the Executables”) is an essential chore. You may run scripts and applications from anywhere in your terminal by adding folders to your PATH environment variable. Understanding how to add folders to your PATH is a useful ability whether you’re a developer, system administrator, or casual user. In this unit, you will understand how to add a directory in Linux and remove a directory to free up space or clean temporary directories undergo.

You will be guided through the procedure by this blog, which will make sure that you can easily access your scripts and tools whenever you need to. An environment variable called PATH tells a Linux system where to look for executable files. The user can execute a command without giving a path thanks to the PATH variable. In this post, we’ll cover adding and removing directories in Linux temporarily and permanently.

Requirements

  • Access to the computer.
  • An editor of text.

What Is PATH In Linux?

The system runs a program when a user enters 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.

How to View the PATH Directories

Use the echo command to print a list of every configured directory in the system’s PATH variable.

echo $PATH

command prints configured directories in path

The result displays default PATH-configured directories. The identical result is produced with the printenv command:

printenv PATH

output shows configured directories

Running on a specific command also reveals the location of the executable. Execute which with whoami, for instance:

which whoami

output shows that executable for whoami is located

The output indicates that whoami’s executable can be found in the /usr/bin/ directory.

How Can I Add a Directory to Linux’s PATH?

To PATH, specific directories are automatically added. Other folders can be added to PATH either temporarily or permanently by users.

Linux: Temporarily Add to PATH

Only the current terminal session is affected when a directory is momentarily added to PATH. The directory is deleted when users close the terminal.

Use the export PATH command to momentarily add a directory to the PATH:

export PATH="/Directory1:$PATH"

command added Directory1 from home directory to PATH

The command changed PATH to include Directory1 from the Home directory. Verify the outcome using:

echo $PATH

output shows directory was added to variable

The result indicates that the variable now contains the directory. Only the current session will be affected by this setting.

Add to Path Permanently in Linux

Step 1: Using Text Editor Open .bashrc File

An example of Vim is given below. Here open the .bashrc file using a text editor.

using export path command temporarily add directory to path

Step 2: Visit to End of the File.

Here In this procedure, the next step is to move to the end of the file.

Step 3: Paste the Export Syntax Here

Once you visit the end of the file, paste the export syntax.

export PATH="/Directory1:$PATH"

paste export syntax at end

Step 4: Store and Leave.

Now, just Save the task you have done and Exit.

Step 5: Run the Script Now.

Run the script or restart the System to put the modifications into effect.

Step 6: In this Step, Confirm the Modifications

run echo path command to verify changes

Run Echo to Confirm the Modifications. 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:

add the directory to PATH for all users

Linux: Removing a Directory from the PATH

To remove a directory from PATH, there isn’t a single command. Nevertheless, several options make it possible.

Medium 1: Leave the Terminal

When a directory is added temporarily to PATH, removing it is easy. The directory can only be added to the terminal during the active session. When the current session expires, the directory is automatically deleted from PATH.

Just close the terminal or you can restart the system, If you want to remove a temporary directory from PATH.

Medium 2. Modify Configuration Files

Use the same procedure to get rid of the directory export string if it was added to the .bashrc or .profile file. In a text editor, open the file, go to the end of the file, and delete the directory.

Medium 3: Going for String Replacement Concept

Use string substitution to remove a directory from the PATH:

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

remove directory from path use string replacement

Only the string from the current session is deleted by the command.

Medium 4: Utilize a One-Liner in this Method

To remove a directory from PATH, you can also use the commands tr, grep, and paste. For illustration:

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

combination of tr, grep and paste to remove directory

Wrapping Up on Adding a Directory to Path in Linux

A quick and effective technique to improve the efficiency of your computing is to add folders to your PATH environment variable. You can make sure that your preferred tools and scripts are easily accessible from the command line by following the instructions provided in this article.

Mastering PATH configuration is a useful skill that will ultimately save you time and effort, whether you’re customizing your development environment or improving the usability of your system. So go ahead and expand your PATH to enhance your command-line experience; it’s a little modification that can have a significant impact.