How to Insert a Directory to PATH in Linux
Last Updated :
31 Dec, 2024
Adding a directory to the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without requiring the full path each time.
When you type a command in the Linux terminal, the shell searches for the corresponding executable file to run it. These executables, such as ls
, grep
, or file
, are stored in specific system directories like /bin
, /usr/bin
, or /usr/local/bin
. But how does the shell know where to look for these files? The answer lies in the $PATH variable, which tells the shell where to search for executable files.
This article explains what the $PATH variable is, why it’s important, and how you can add or modify directories in the PATH to customize your Linux environment.
What is $PATH in Linux?
The $PATH variable is an environment variable that contains a colon-separated list of directories. It specifies where the shell should look for executable files when you type a command.
To check the current directories in your $PATH, run:
echo $PATH
Example Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The shell searches these directories in order. If two files with the same name exist in different directories, the shell will execute the one in the directory listed first in the $PATH.
Why Add a Directory to $PATH?
Adding a directory to $PATH makes it easier to run scripts or programs stored in custom directories. For instance:
- You may want to create a directory for your personal scripts (e.g.,
~/bin
) and execute them without typing the full path. - Some applications might install their binaries in uncommon locations not included in the default $PATH.
How to Add a Directory to $PATH in Linux?
1. Temporary Addition (For Current Session Only)
To temporarily add a directory to your $PATH, use the export
command:
export PATH="$PATH:/path/to/directory"
This change lasts only for the current terminal session. Once you close the terminal, the modification is lost.
Example:
If you have a directory ~/scripts
where your custom scripts are stored, run:
export PATH="$PATH:$HOME/scripts"
You can now execute your scripts without typing the full path.
2. Permanent Addition (For All Future Sessions)
To make the change permanent, add the directory to the $PATH variable in the appropriate shell configuration file:
- For a single user: Edit the user's shell configuration file (e.g.,
~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
). - For all users: Edit global configuration files like
/etc/environment
or /etc/profile
.
Steps to Add to ~/.bashrc
:
- Open the file in a text editor:
nano ~/.bashrc
- Add the following line at the end of the file:
export PATH="$PATH:/path/to/directory"
- Save the file and apply the changes:
source ~/.bashrc
Verify the Change: Check if the directory has been added by running:
echo $PATH
How to Remove a Directory from $PATH?
Over time, you may need to remove a directory from the PATH for various reasons, such as cleaning up unused entries or resolving conflicts. While there's no single command to directly remove a directory from PATH, several methods can help you achieve this effectively:
Method 1: Exit the Terminal or Reboot the System
If the directory was added to the PATH temporarily during the current session, it will automatically be removed once the terminal is closed or the system is rebooted. This approach is useful if you made a temporary change using the export
command, such as:
export PATH="$PATH:/path/to/temp-directory"
Simply close the terminal or reboot to revert the PATH variable to its previous state.
Method 2: Edit Configuration Files for Permanent Changes
If the directory export string was added to the .bashrc
or .profile
file, remove it using the same method. Open the file in a text editor, navigate to the end of the file, and remove the directory. Save the changes and reload the configuration file using the source
command. This ensures the changes take effect immediately without restarting the system.
Method 3: String Replacement in the Current Session
For a current session-only removal, use string replacement to exclude the directory from PATH:
export PATH=${PATH//:/path/to/remove}
${PATH//:/path/to/remove}
replaces occurrences of /path/to/remove
in the current PATH value.- The updated PATH is applied temporarily and reverts when the terminal is closed.
Method 4: Using a One-Liner with tr
, grep
, and paste
To remove a directory from PATH programmatically, use a combination of commands:
export PATH="$(echo $PATH | tr ':' '\n' | grep -v '/path/to/remove' | paste -s -d ':')"
This method is ideal for scripting and ensures the change affects only the current session.
Conclusion
Inserting a directory into the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without specifying the full path. This can be done temporarily using the export
command or permanently by modifying configuration files like ~/.bashrc
.
Managing the PATH effectively—by adding or removing directories—ensures a streamlined workflow, easy access to custom scripts, and a clean, efficient environment for seamless command execution.
Similar Reads
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
grep command in Unix/Linux The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen. Syntax of grep Command in Unix/LinuxThe basic syntax of the `grep` command is as follows:grep [opt
6 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
ZIP command in Linux with examples In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
What is Linux Operating System The Linux Operating System is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly a
13 min read