Home / Exciting tips / Linux / How to add directories to path on Linux

How to add directories to path on Linux

On Linux, $PATH defines the standard directories in which executable programs can run from on the system through the terminal. These directories are “/usr/bin”, “/usr/local”, “/usr/sbin”, and a few other ones (depending on what Linux distribution you use). If you want to run programs outside of these directories, the directory needs to be added to the $PATH.

Back up .bashrc

Adding directories to $PATH on Linux is done by editing the “.bashrc” file of an individual user. However, before starting the editing process, you must make a backup of your “.bashrc” file, as it defines the terminal variables for the user account.

Using the cp command, make a complete copy of the “.bashrc” file in your home directory (~), and place the backup in “Documents,” with the “.bak” file extension.

Note: don’t want to store your backup in the “Documents” directory? Try “Desktop,” a cloud sync folder like “Dropbox” or something similar.

cp ~/.bashrc ~/Documents/bashrc.bak

Edit .bashrc

With the “.bashrc” file backed up to your “Documents” folder, editing can begin. Using the Nano command, open up the “.bashrc” file (located in your home directory) for editing purposes.

Warning: do not open “.bashrc” with sudo, as you will open up the Root account’s “.bashrc” file and not your own user account’s file!

nano -w ~/.bashrc

Inside of the Nano editor, make your way to the bottom of the file. Then, write in export followed by PATH=$PATH:directory. So, for example, to run a program directly from the “programs” folder in “/home/derrik/” I’d write the following code into Nano.

export PATH=$PATH:/home/derrik/programs/

For your purposes, copy the command example below and replace “directory” with the exact location of the program folder, or script folder so that it can be added to your path. Keep in mind that it is possible to add multiple $PATH lines, and there is no limit, so feel free to add as many locations as you like!

export PATH=$PATH:/location/to/program/or/script/folder/

When you’ve added as many $PATH lines to the “.bashrc” file as you need, press the Ctrl + O button on the keyboard to save the edits. Then, close Nano with Ctrl + X.

After exiting the Nano text editor, your $PATH will not be updated right away. The reason that updating is not instant is the terminal session doesn’t auto-update and needs to be refreshed. To refresh the terminal session, close the terminal and re-open it. From there, you can confirm your new $PATH locations have been added by running the following command in a terminal.

echo $PATH

Adding locations to the $PATH for other users

In addition to adding locations to the $PATH for your user account on the Linux system, you may want to add items to the $PATH of another user account. Here’s how to do it.

First, use the su command to log into the user in which you wish to modify the Linux $PATH.

su username

After logging in to the user account, use the cp command to copy their “.bashrc” file to “Documents” for safekeeping.

cp .bashrc ~/Documents/bashrc.bak

With the backup taken care of, open up the “.bashrc” file in the Nano text editor with the command below.

nano -w ~/.bashrc

Move to the bottom of the “.bashrc” file, and write in the command below. Keep in mind that you will need to change “/location/to/program/or/script/folder/” to suit your own needs.

export PATH=$PATH:/location/to/program/or/script/folder/

Repeat this process to add as many locations to the $PATH as you like. When done, save the edits using Ctrl + O, and exit with Ctrl + X. Next time the user logs in, they’ll be able to run things in $PATH from the new locations you’ve added.

Temporarily add directories to $PATH on Linux

Permanently adding individual directories to the $PATH on Linux by editing the “.bashrc” file is useful if you have specialized programs you’d like to run all the time. However, if you only need to run something once for testing, or just tinkering, it doesn’t make sense to add something to the $PATH on Linux permanently.

An alternative to permanent custom directories in the $PATH is by making use of the export command in the terminal to temporarily add a location to the $PATH.

Open up a terminal window and write in the export command, followed by ATH=$PATH:directory. For example, to add in a shell-script directory in “Documents” temporarily, you’d do:

export PATH=$PATH:/home/derrik/Documents/shell-scripts/

Upon running this command, the terminal session you are using will have the new temporary path location. The temporary $PATH location will stay accessible on your Linux system until the system reboots.

Remove additions to $PATH

Looking to get rid of the custom locations added to your $PATH on Linux? Follow the steps below to undo the edits.

Note: if you need to restore the defaults for another user, log in with su username before following the steps below.

Step 1: Delete the “.bashrc” file using the rm command.

rm ~/.bashrc

Step 2: Rename the “bashrc.bak” file as “.bashrc” in the “Documents” folder, and place it in the home directory (~).

mv ~/Documetns/bashrc.bak ~/.bashrc

Step 3: Restart your terminal session for the changes to take effect.

Step 4: Run the $PATH command to check to see if your custom edits are gone.

$PATH

About excitingtips

Check Also

How to check CPU temperature on Linux

On Linux, if your CPU is running hot, you won’t be able to open up …

Leave a Reply

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