How to Add a Directory to PATH in Linux?
Introduction
Linux is a powerful and versatile operating system that offers a wide range of features and customization options. One of the principal elements of working with Linux is understanding how the PATH variable functions. The PATH variable is an environment variable that stores a list of directories in which the operating system looks for files that can be executed when you implement a command in the terminal.
With this blog post, we will explore the concept of PATH in Linux and provide detailed instructions on how to add a directory to a path in Linux. Additionally, we will cover methods to temporarily or permanently modify the PATH variable, as well as ways to remove directories from it.
What Is Linux PATH?
The PATH in Linux plays a fundamental role in the operating system’s environment. It acts as a roadmap for the system to locate executable files when a command is entered in the terminal. The PATH variable consists of a colon-separated list of directories where the OS finds these executables.
When you execute a command in the terminal, the Linux kernel examines each directory listed in the PATH variable, from left to right, in search of the corresponding executable file. If it finds a matching file, it executes the command. If the file is not found in any of the specified directories, the system returns an error indicating that the command is not recognized.
Linux, by default, includes a set of standard directories in the PATH variable. These directories are carefully chosen to ensure that essential system executables are readily accessible from any location within the system. Some of the commonly included directories are:
- /bin: This directory contains fundamental executables required for basic system operations. It includes essential commands such as ls, cp, and rm.
- /usr/bin: The /usr/bin directory contains a wide range of user executables, including various command-line tools and applications installed by the system or the user. Common commands like gcc, python, and git are generally found in this directory.
- /usr/local/bin: This directory is intended for executables installed locally on the system by the user or system administrator. It is often used to store programs that are not portion of the default system installation. When you compile and install software from source code, the executables are usually placed in this directory.
These standard directories in the PATH variable ensure that frequently used commands and utilities are easily accessible, regardless of your current working directory. However, as you explore Linux and install additional software, you may come across situations where you need to add a directory to PATH in Linux which is new.
By adding directories to the PATH variable, you can make the executables within them globally accessible without having to specify their full paths each time you run a command. This flexibility allows you to streamline your workflow and improve productivity by eliminating the need to constantly navigate to specific directories or provide explicit paths for commands.
How to View the Directories in PATH?
Before we dive into how to add a directory to a path in Linux or remove it from the path in Linux, it is crucial to know how to view the current directories included in it. To view the directories, open a terminal and execute the following command:
echo $PATH
This command will display the list of directories separated by colons.
How Do I Add a Directory to PATH in Linux?
There are two primary methods to add a directory to the PATH variable in Linux: temporarily or permanently. Depending on your requirements, you can pick the method that meets your necessities aptly.
Linux: Add to PATH Temporarily
Adding a directory to the PATH temporarily means that the changes will be valid uniquely for the current session. Once you close the terminal, the modifications to the PATH variable will not persist.
To add a directory temporarily, execute the below steps:
- Open up a terminal on your Linux system.
- Use this given command to append the desired directory to the existing PATH variable:
export PATH=$PATH:/path/to/directory
Replace “/path/to/directory” with the actual path of the directory you want to add. This command appends the specified directory to the existing PATH variable for the current session.
When you add a directory to PATH in Linux temporarily, you can use the executables within that directory without having to provide their full paths. However, remember that once you close the terminal, the changes will be discarded, and you will need to repeat the process if you want to add the directory again in future sessions.
Linux: Add to PATH Permanently
To make the changes to the PATH variable permanent, you need to modify the configuration files of your Linux distribution. This method ensures that the added directory remains in the PATH across all terminal sessions.
The specific file you need to edit may vary depending on the distribution and shell you are using. Here are the common configuration files for popular shells:
- Bash: ~/.bashrc or ~/.bash_profile
- Zsh: ~/.zshrc
- Fish: ~/.config/fish/config.fish
To add directory to PATH in Linux permanently, follow these steps:
- Open the terminal on your Linux system.
- Identify the appropriate configuration file for your shell.
- Open the file in a text editor (e.g., nano, vi, gedit).
- Add the following line at the end of the file:
export PATH=$PATH:/path/to/directory
Replace “/path/to/directory” with the real directory path you wish to append.
5. Save this file and then close the text editor.
The changes you made to the configuration file will take effect when you open a new terminal session or restart your computer. Once applied, the added directory will be included in the PATH variable by default, allowing you to access the executables within that directory from any location in the system.
By adding a directory permanently to the PATH, you save time and effort by eliminating the need to manually modify the PATH in every session. The executables in the added directory become globally accessible, providing convenience and flexibility in your day-to-day tasks.
Also Read: How to Start, Stop, and Restart Nginx in Linux? (2 Methods)
Remove Directory from PATH in Linux
If you no longer want a specific directory to be included in the variable PATH In Linux, there are multiple methods to remove it.
Method 1: Exit the Terminal
The simplest way to remove a directory from the PATH variable temporarily is to close the terminal session. When you open a new terminal session, the PATH variable tend to reset to its default value.
Method 2: Edit Configuration Files
To permanently remove a directory from the PATH variable, you can edit the appropriate configuration file as mentioned earlier (e.g., ~/.bashrc or ~/.bash_profile).
Locate the line in the file that includes the directory you wish to remove, and delete that line. Save this file and then close the text editor. The changes will take effect upon opening a new terminal session or restarting your computer.
Method 3: Apply the String Replacement Concept
Another approach is to use the string replacement concept in Linux to remove the directory from the PATH variable. Open a terminal and enter the following command:
export PATH=${PATH//:/}
This command removes all occurrences of colons from the PATH variable, effectively eliminating all directories from it. However, keep in mind that this method removes all directories and should be used with caution.
Method 4: Use a One-Liner
An alternative method to remove a specific directory from the PATH variable is by using a one-liner command in the terminal. Execute the given command:
export PATH=$(echo $PATH | tr ':' '\n' | grep -v "/path/to/directory" | tr '\n' ':')
Replace “/path/to/directory” with the real directory path you wish to discard. This command uses a combination of string manipulation and filtering techniques to remove the specified directory from the PATH variable.
Also Read: Steps to Unzip/Extract tar.gz Files in Linux Using Command Line
Conclusion
Understanding how the PATH variable functions in Linux is essential for efficient command execution. By keeping up with the instructions discussed in this guide, you can know exactly how to add a directory to PATH in Linux temporarily or permanently. Additionally, you have learned various methods to remove directories from the PATH variable, allowing you to fine-tune your Linux environment according to your needs. Enjoy the flexibility and power that Linux provides as you continue to explore its vast possibilities.