Linux Commands

Add directory to path in Linux

Every time you run a command on a Linux terminal, you are basically commanding the shell to run an executable program bearing the given name. Executable programs, including simple programs such as ls, mkdir, touch, and find, reside on special directories on the filesystem. In Linux, directories that accommodate such executable programs include:


A file with executable permissions in one of those directories can be executed from any location within the Linux terminal.

So, the question begs, how does the Linux shell know where to look for the programs? It doesn’t start the search from the current directory or anywhere random in the filesystem. The shell relies on the $PATH variable.

What is the $PATH variable?

$PATH is an environment variable that tells the shell where to locate the executable file. There are various directories defined in the $PATH variable. To display directories in your $PATH, run the command:

$ echo $PATH

To locate the directory where a command executable is located, use the which command as follows

$ which command

For example, to locate where the executable for the pwd command, run the command:

$ which pwd

From the output, we can see that the executable resides in the /bin directory. For the touch command, the executable resides in the /usr/bin directory.

How to add a directory to $PATH

Suppose you have a simple shell script called that prints “Hello World” placed in the /home/james/files directory. By default, the directory is not yet defined in the $PATH variable. To run the script from any location or directory, you need to specify the absolute path to the script. Now, this can be cumbersome and time-consuming.

To run the script globally (regardless of your location in the filesystem) without specifying the full path to the script, you need to add the directory containing the script to the $PATH variable using the syntax below.

$ export PATH=$PATH:/path/to/directory/with/file

In this case, the command will be:

$ export PATH=$PATH:/home/james/files

You should now be in a position to call or run the script from any directory within your Linux system without specifying the absolute path to the script as shown.

How to permanently add the directory to the $PATH variable

The path that we have just defined to $PATH is only temporary and doesn’t persist when you close the terminal or reboot your system. It only works in the current shell session. If you exit and launch another session, you will bump into the error as shown.

To persist the changes, define the $PATH variable in the ~.bashrc configuration file. To accomplish this, open the configuration file.

$ sudo vim   ~/.bashrc

Then add the line as shown.

$ export PATH=”$PATH:/home/james/files”

After that, save and exit. To load the new changes, invoke the source command as follows:

$ source ~/.bashrc

To verify the addition of the directory to $PATH, execute the command as shown.

$ echo $PATH

Wrapping up

And there you have it, guys! We have managed to add the directory to $PATH on Linux successfully. As you have seen, it’s quite convenient and straightforward, especially if you will be calling the script or application regularly from the shell. The same commands will work for any Linux flavor.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.