| by Arround The Web | No comments

How to Add a Directory to the Path on Rocky Linux 9

Adding a directory to the PATH allows a Linux user to access the files or scripts without specifying their full path in the terminal. In simple words, adding a directory to the PATH makes it possible for the users to run the scripts of that directory from anywhere in the terminal. For example, “script.sh” is located in the “Documents” directory.

Now, if you add the ~/Documents or /home/user/Documents to the PATH, you can access the “script.sh” by only executing its name without specifying the directory. Hence, adding the directory to the PATH can be a good idea because it helps you execute the files conveniently. In this short guide, we will explain the methods to add a directory to the PATH on Rocky Linux 9.

How to Add a Directory to the PATH on Rocky Linux 9

To add the directory to PATH, you must change the bashrc file. Let’s take an example to add the path of the “script.sh” which is located in the /Documents/My_Script directory. First, run the following command to open the bashrc in the terminal:

nano ~/.bashrc


The given command opens the text editor. Add the following line at the end of the text:

export PATH="/path/to/directory:$PATH"

Here, the “script.sh” is located in the /Documents/My_Script directory so we can execute the following command:

export PATH="/home/prateek/Documents/My_Script:$PATH"

Once you are done, save the file and then run the following command to save the changes successfully:

source ~/.bashrc

The previous command executes the content of the bashrc file. Essentially, the source ~/.bashrc command is used to reload the bashrc to make the changes successfully. Furthermore, you can confirm the changes through the following command:

echo $PATH

Now, let’s try to run the following command to execute the script in the terminal:

script.sh

If you get the error, close the terminal and execute the command again. Moreover, you can run the following commands to open the script with no error:

cd /opt/

cd /var/log/

How to Remove a Directory to the PATH

If you want to remove the directory from the PATH, open the bashrc file again and then remove the directory path:

nano ~/.bashrc

Once you are done, save the file and then reload the changes through the following command:

source ~/.bashrc

Conclusion

This is how you can easily add the directory to the PATH on Rocky Linux 9 and execute the file from the terminal. We also explained a quick method to remove the directory from the bashrc file. Adding a directory to the PATH helps you run the scripts or programs without specifying their path. Hence, you can use this method to save up your efforts of specify the path of the programs before executing them.

Share Button

Source: linuxhint.com

Leave a Reply