Adding directories to your PATH - gHacks Tech News

Adding directories to your PATH

One of the things I really like about Linux is how it handles a file's ability to be executable. If you create a script and you want that script to be executable, you just set the permissions as such. Say you've created the script myscript.sh and you've placed it in your home directory. To execute that script all you would have to do is issue the command ~/myscript.sh. That's all fine and good, but what if you wanted that script to be globally accessible for yourself? And let's say you don't want to copy that file, for whatever reason, to either the /usr/bin or the /usr/local/ directory. What would you do? That's simple - you could place that file within a subdirectory of your home directory and add that directory to your PATH.

You see, any directory in your users's PATH is global. That means you can issue a command by simply entering the command - you do not have to enter the full path to the command. That is why you don't always have to enter /usr/bin/ls and you can just enter ls.

In this article, we step back for a bit of Linux basics and learn how to add directories to your path.

See what's already there

If you are curious to know what is already in your PATH you can do so by entering the command echo $PATH. You should see something like:

/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

The above is pretty standard for a default Linux path. What you are seeing is global paths, each separated by a ":". Any file in:

  • /usr/bin
  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /usr/games

will be global for the user.

Adding a new directory

There are three ways to add a new directory to your path. Let's take a look at them individually. The first method only adds the new directory to the PATH temporarily. This is great for testing purposes, but know that the minute you log out that directory is removed from the users' path.

Let's say you have the directory ~/scripts that you want to add temporarily. To do this open up a terminal window and issue the command:

PATH=$PATH:/home/USERNAME/scripts

Where USERNAME is the actual name of the user you are using. Now if you issue the command echo $PATH that new directory will be listed. Log out and log in and issue that same command and the directory will be gone.

Now let's say you want to make that addition permanent for the user. This was typically done in the users' ~/.bash_profile file. You might notice that you don't have such a file. Newer Ubuntu releases have opted for the ~/.profile file instead. I still prefer to create a ~/.bash_profile for these purposes.  So if you don't have a ~/.bash_profile create one and open it. If you have a ~/.bash_profile open it. and add a line like:

PATH="$HOME/bin:$PATH:/home/USER/scripts:"

Where USER is the actual user name.

You might think you would have to log out and log back in. You don't. Just issue the command source .bash_profile and the changes will take effect.

You can also add this path to every users PATH by adding the same line to the /etc/profile. You probably won't find a reference to PATH in that file. So what you have to do is add the following lines to the end of that file:
PATH=$PATH:/home/USER/scripts
export PATH

Where USER is the actual user name. Of course you probably don't want to add a directory of one user to everyone's PATH, so you might want to create a new directory (like /data/scripts) and add that the to /etc/profile. Once you are done save that and the next time someone logs in they will have that new directory in their PATH.

Final thoughts

Linux is all about flexibility. As you can see it is possible to really stretch the usability of your directory structure to make it work for you instead of against you. Adding directories to your PATH variable allows you to create scripts that can be run globally, and by anyone.

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. REz said on June 7, 2010 at 3:11 pm
    Reply

    windows does it too.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.