Linux Commands Ubuntu

How to Add a Directory to a $PATH in Linux/Ubuntu?

How to Add a Directory to a $PATH in Linux Ubuntu

A path in an environment variable that includes directories such as bin, sbin, etc. These directories contain executable files. A path guides the Linux/Ubuntu shell where to look for executable files. This enables a user to execute a command from anywhere in the system without stating the path provided that the directory is added to the $PATH.

This article will demonstrate how to add and remove a directory to and from a $PATH on Linux/Ubuntu 22.04 LTS. This article will discuss:

  • How to View Configured Directories in $Path?
  • How to Search the Path of an Executable in Linux/Ubuntu?
  • How to Add/Include a Directory to Path via Terminal?
  • How to Add/Include a Directory to the Path by Modifying .bashrc File?
  • How to Add/Include a Directory to the Path by Modifying .profile File?
  • How to Add/Include a Directory to the Path by Modifying the/etc/environment File?
  • How to Remove/Delete a Directory from Path via Terminal?
  • How to Remove/Delete a Directory from Path via grep, Paste, and tr Utilities?
  • How to Remove/Delete a Directory from Path by Editing .bashrc, .profile, and /etc/environment Files?
  • How to Remove/Delete a Directory from Path by String Replacement?

How to View Configured Directories in $Path?

The configured directories in the Linux/Ubuntu system can be displayed by echo by

$ echo $PATH

Alternatively, the configured directories can also be displayed by using printenv commands as follows:

$ printenv PATH

How to Search the Path of an Executable in Linux/Ubuntu?

The path of executables can be seared by using which command. The following commands display the paths of echo and apt command line utilities.

$ which echo

$ which apt

From the above image, it can be observed that echo and apt paths are /usr/bin/echo and /usr/bin/apt respectively.

How to Add/Include a Directory to Path via Terminal?

A directory is temporarily added to $PATH via Terminal by the following syntax:

$ export PATH="<Directory>:$PATH"

For example if we want to add directory /home/linuxuser/test/path, the following command will be used:

$ export PATH="$HOME/test/path:$PATH"

We can verify the added directory by

$ echo $PATH

From the above image, it can be observed that the path is successfully added. Additionally, these changes in the path are temporary and will only be in effect for the current terminal session.

How to Add/Include a Directory to Path by Modifying .bashrc File?

The following steps are followed to add a directory: /home/linuxuser/test/path to a path permanently:

  • Open the “.bashrc” file in a Nano Text Editor:
$ nano .bashrc

  • Scroll to the end of the file and insert the following line:
$ export PATH="$HOME/test/path:$PATH"

  • Save and Exit .bashrc file by pressing [Ctrl+O] and [Ctrl+X] respectively.
  • Reboot the system or run the following script for the changes to take effect immediately:
$ source ~/.bashrc

  • The addition of Directory /home/linuxuser/test/path can be verified by echo command by
$ echo $PATH

The above image confirms that the desired directory is added to $PATH. Additionally, a directory is added for current users only by modifying the .bashrc file.

How to Add/Include a Directory to Path by Modifying .profile File?

The following steps are followed to add a directory: /home/linuxuser/test/path permanently by modifying the .profile file.

  • Open the “.profile” file in Text Editor, for example, Nano text editor:
$ sudo nano .profile

  • Scroll to the end of the file and insert the following line:
$ export PATH="$HOME/test/path:$PATH"

  • Save and Exit .profile file by pressing [Ctrl+O] and [Ctrl+X] respectively.
  • Reboot the system in order for the changes to be in effect.
  • The addition of Directory /home/linuxuser/test/path can be verified by echo command by

The above image confirms that the desired directory is added to $PATH. Additionally, a directory is added for all users only by modifying the .profile file.

How to Add/Include a Directory to Path by Modifying /etc/environment File?

The following steps are followed to add a directory: /home/linuxuser/test/path permanently by modifying the /etc/environment file.

  • Open the “/etc/environment” file in Text Editor, for example, Nano text editor:
$ sudo nano /etc/environment

  • Add the following line at the end of the file.
$ PATH="PATH/home/linuxuser/test/path"

  • Save and Exit .profile file by pressing [Ctrl+O] and [Ctrl+X] respectively.
  • Reboot the system in order for the changes to be in effect.
  • The addition of Directory /home/linuxuser/test/path can be verified by echo command by
$ echo $PATH

The above image confirms that the desired directory is added to $PATH. Additionally, modifying the “/etc/environment” file sets system-wide environment variables.

How to Remove/Delete a Directory from Path via Terminal?

If the directory is added using export PATH=”<Directory>:$PATH” then these changes are temporary and will no longer exist after the terminal is restarted.

How to Remove/Delete a Directory from Path via grep, Paste, and tr Utilities?

A directory “/home/linuxuser/test/path” is removed from $PATH by using a combination of tr, grep, and paste Command by

$ export PATH="$( echo $PATH| tr : '\n' |grep -v $HOME/test/path | paste -s -d: )"

The above image confirms that the directory “/home/linuxuser/test/path” is removed from the $PATH.

How to Remove/Delete a Directory from Path by Editing .bashrc, .profile, and /etc/environment Files?

A directory can be permanently removed from $PATH by modifying the .bashrc, .profile, and /etc/environment Files. For example, if we have to remove a directory “/home/linuxuser/test/path”, we can edit .bashrc and .profile files by:

  • Scrolling to the end of the files and removing the line below:
$ PATH="PATH/home/linuxuser/test/path"

Additionally, if we have to remove a directory “/home/linuxuser/test/path”, we can edit /etc/environment file by:

  • Remove the following line at the end of the file
$ PATH="PATH/home/linuxuser/test/path"
  • Save and Exit .bashrc, .profile, and edit /etc/environment files by pressing [Ctrl+O] and [Ctrl+X] respectively.
  • Reboot the system in order for the changes to be in effect.

How to Remove/Delete a Directory from Path by String Replacement?

String Replacement can be used to remove a directory “/home/linuxuser/test/path” from $PATH by

$ export PATH=${PATH/'/home/linuxuser/test/path'/}

The above image confirms that the directory “/home/linuxuser/test/path” is removed from the $PATH.

Conclusion

A directory is added to a $PATH permanently by modifying either of the three fiels:.bashrc file, .profile file, and /etc/environment file. Additionally, a directory is added to a $PATH temporarily by using the command “export PATH=”<directorypath>:$PATH”. This article demonstrated how to add a directory temporarily and permanently to $PATH as well as how to remove a directory on Linux/Ubuntu 22.04 LTS.

 

Similar Posts