Monday, September 20, 2021

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

You will eventually need to be able to run commands from directories other than standard. You’ll need to add these directories to your $PATH when that happens. Jack Wallen shows you how.

Linux logo in purple

Image: PegasuStudio/Shutterstock

Your Linux PATH is the way you define which directories can be run globally. You can execute any executable file that is located in a directory you have configured to be in your Linux Path. This allows you to run commands in the /usr/bin directory from your home directory (or any other location). 

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

The Linux PATH is the standard entry list. It contains the usual entries like /usr/bin/ and /usr/sbin/. What happens if you have a different directory that you need to be capable of running commands from? You will need to manually add these directories to your PATH. 

How can you do this? Let me show you. 
Let’s say that you have a directory called SCRIPTS under your home directory. Let’s add it to the PATH. 

  1. Log in to your Linux computer and open a terminal window. 
  2. Edit your.bashrc file with the command nano ~/.bashrc
  3. Scroll down to the bottom and add the following. PATH=”~/SCRIPTS:$PATH”.
    It is very important to include $PATH as it ensures that the standard directories remain within your path. Without this, SCRIPTS would be the only directory in your Path. 
  4. Save and close the file. 
  5. Close the terminal and reopen it. 

You can now run any executable found in the SCRIPTS directory from any location in the filesystem hierarchy. 

SEE: Rust: What developers must know about this programming language (free pdf)(TechRepublic).

That’s how you can add directories in your PATH. This little trick will prove very handy, especially if your bash scripts are not to be saved in the same directory.

Subscribe to TechRepublic How to make YouTube Tech workJack Wallen offers the most up-to-date tech advice for business professionals.

Also, see

    Leave a Reply