• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Add Directory to PATH in Linux

Written by Admin, Updated On May 21, 2020
terminal
How to Add a Directory to PATH in Linux

When you want to run a Linux tool from a non-standard directory, you need to add that directory to your $PATH. In Linux most common executable like ls, find, file and more are found in /bin, /sbin, /usr/sbin, /usr/local/bin and /usr/local/sbin directories. Any executable file stored in these directories can run from any location. It just so happens those directories are a part of what is called the user $PATH. In this tutorial we will learn how to add directory to your PATH in Linux systems.

Once you type a command, the shell searches through all directories specified in the user $PATH variable for an executable file of that name.

What is $PATH in Linux#

The $PATH is a list of directories that inform the shell that in which directories to search for executable files.

To see what’s in your $PATH right now, type this into a terminal:

echo $PATH

It will print something like following:

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

When you have multiple executable files having the same name and located in different directories then shell will consider the first directory which comes first in the $PATH.

Adding a Directory to your $PATH#

Sometimes you have requirement to run your executable files on shell without specifying the absolute path. To do this you just need to add the directory to your $PATH.

For example, you have a directory work located in your home directory and a executable file is stored in it. Run the following command to add the directory to your $PATH :

export PATH="$HOME/work:$PATH"

In above command we are using the export command to add a directory to the $PATH. The directory will be included in the list of file system locations where shell searches.

You can now run your scripts without specifying the full path of executable file. Simply type the executable script name.

Remember that, this change is temporary change and valid only for the current shell session.

To convert the change in permanent, you need to define the $PATH variable in the shell configuration files.

Adding directory permanently to your $PATH will depend upon which shell you use. Generally, Linux distributions are running Bash. You can know your Shell by issuing the echo $SHELL command.

For example, if you are using Bash, you have to set the $PATH variable in the ~/.bashrc file and if you are using Zsh the file name is ~/.zshrc.

In this example, we are setting the variable in the ~/.bashrc file.

Edit the file with your text editor and append the following line at the end of file:

nano ~/.bashrc
export PATH="$HOME/work:$PATH"

Save and close the file.

Next, close and re-open your terminal and type echo $PATH command to check the entered path.

echo $PATH

Conclusion#

It is very simple and straightforward to add new directory to PATH variable. Steps are same for any Linux distribution, like Ubuntu, CentOS, RHEL, Debian and Linux Mint.

If you have any question or suggestion, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Find Files in Linux Using the Command Line
Next Article   How to Install PostgreSQL on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook