Linux 101: How to add directories to your Linux $PATH

3 years ago 312

At some point, you're going to want to run commands from non-standard directories. When that happens, you'll want to add those directories to your $PATH. Jack Wallen shows you how.

Linux logo in purple

Image: PegasuStudio/Shutterstock

Your Linux PATH is how you define the directories for which commands can be run globally. In other words, if you have an executable file in a directory that is configured to be in your PATH, you can run that executable from anywhere in the Linux file structure. This is what makes it possible to run commands in /usr/bin from your home directory (or anywhere, for that matter). 

SEE: 5 Linux server distributions you should be using (TechRepublic Premium)

Out of the box, the Linux PATH contains the usual entries, such as /usr/bin/, /usr/sbin/, /usr/local/bin, and so on. But what happens if you have a non-standard directory from which you need to be able to execute commands? That's when you have to manually add those directories to the PATH. 

How do you do that? Let me show you. 
Let's say you have a directory called SCRIPTS in your home directory. Let's add that to the PATH. 

  1. Log into your Linux machine and open a terminal window. 
  2. Open your .bashrc file for editing with the command nano ~/.bashrc
  3. Scroll to the bottom of that file and add the following: PATH="~/SCRIPTS:$PATH".
    It's very important to include the $PATH portion, as that makes sure the standard directories remain in your path (otherwise, the only directory in your PATH would be SCRIPTS and that would not be good). 
  4. Save and close the file. 
  5. Close and reopen the terminal. 

At this point, you can run any executable, found in the SCRIPTS directory, from anywhere in the filesystem hierarchy. 

SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)

And that's how you add new directories to your PATH. This little trick will come in very handy, especially when you start writing your own bash scripts that you don't want to be saved in common directories.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Open Source Weekly Newsletter

You don't want to miss our tips, tutorials, and commentary on the Linux OS and open source applications. Delivered Tuesdays

Sign up today

Also see

Read Entire Article