{"id":992,"date":"2023-05-31T06:41:42","date_gmt":"2023-05-31T06:41:42","guid":{"rendered":"https:\/\/www.dedicatedcore.com\/blog\/?p=992"},"modified":"2024-10-19T09:02:07","modified_gmt":"2024-10-19T09:02:07","slug":"add-directory-path-linux","status":"publish","type":"post","link":"https:\/\/www.dedicatedcore.com\/blog\/add-directory-path-linux\/","title":{"rendered":"How to Add a Directory to Path in Linux"},"content":{"rendered":"<p>For anyone working with command-line tools and scripts, managing the system&#8217;s PATH (short for &#8220;Path to All the Executables&#8221;) is an essential chore. You may run scripts and applications from anywhere in your terminal by adding folders to your PATH environment variable. Understanding how to add folders to your PATH is a useful ability whether you&#8217;re a developer, system administrator, or casual user. In this unit, you will understand how to add a directory in Linux and remove a directory to <a href=\"https:\/\/www.dedicatedcore.com\/blog\/remove-directory-linux-with-rm-rmdir-commands\/\" target=\"_blank\" rel=\"noopener\">free up space<\/a> or clean temporary directories undergo.<\/p>\n<p>You will be guided through the procedure by this blog, which will make sure that you can easily access your scripts and tools whenever you need to. An environment variable called PATH tells a Linux system where to look for executable files. The user can execute a command without giving a path thanks to the PATH variable. In this post, we&#8217;ll cover adding and removing directories in Linux temporarily and permanently.<\/p>\n<p><strong>Requirements<\/strong><\/p>\n<ul>\n<li>Access to the computer.<\/li>\n<li>An editor of text.<\/li>\n<\/ul>\n<h4>What Is PATH In Linux?<\/h4>\n<p>The system runs a program when a user enters a command on the terminal. Linux must therefore be able to find the proper program. The system is told where to look for a program to run by PATH, which specifies program directories.<\/p>\n<h4>How to View the PATH Directories<\/h4>\n<p>Use the echo command to print a list of every configured directory in the system&#8217;s PATH variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">echo $PATH<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-997\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories.png\" alt=\"command prints configured directories in path\" width=\"800\" height=\"171\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories-768x164.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-view-directories-700x150.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The result displays default PATH-configured directories. The identical result is produced with the printenv command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">printenv PATH<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-998\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command.png\" alt=\"output shows configured directories\" width=\"800\" height=\"170\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command-768x163.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/printev-path-command-700x149.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>Running on a specific command also reveals the location of the executable. Execute which with whoami, for instance:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">which whoami<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-999\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable.png\" alt=\"output shows that executable for whoami is located\" width=\"800\" height=\"132\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable-300x50.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable-150x25.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable-768x127.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable-100x17.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/which-whoami-command-executable-700x116.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The output indicates that whoami&#8217;s executable can be found in the \/usr\/bin\/ directory.<\/p>\n<h2>How Can I Add a Directory to Linux&#8217;s PATH?<\/h2>\n<p>To PATH, specific directories are automatically added. Other folders can be added to PATH either temporarily or permanently by users.<\/p>\n<h3>Linux: Temporarily Add to PATH<\/h3>\n<p>Only the current terminal session is affected when a directory is momentarily added to PATH. The directory is deleted when users close the terminal.<\/p>\n<p>Use the export PATH command to momentarily add a directory to the PATH:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">export PATH=\"\/Directory1:$PATH\"<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1000\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory.png\" alt=\"command added Directory1 from home directory to PATH\" width=\"800\" height=\"130\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory-300x49.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory-150x24.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory-768x125.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory-100x16.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-command-add-directory-700x114.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The command changed PATH to include Directory1 from the Home directory. Verify the outcome using:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">echo $PATH<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1001\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory.png\" alt=\"output shows directory was added to variable\" width=\"800\" height=\"170\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory-768x163.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/echo-path-terminal-home-directory-700x149.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The result indicates that the variable now contains the directory. Only the current session will be affected by this setting.<\/p>\n<h2>Add to Path Permanently in Linux<\/h2>\n<h3>Step 1: Using Text Editor Open .bashrc File<\/h3>\n<p>An example of Vim is given below. Here open the .bashrc file using a text editor.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1002\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim.png\" alt=\"using export path command temporarily add directory to path\" width=\"800\" height=\"110\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim-300x41.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim-150x21.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim-768x106.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim-100x14.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/open-bashrc-vim-700x96.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<h3>Step 2: Visit to End of the File.<\/h3>\n<p>Here In this procedure, the next step is to move to the end of the file.<\/p>\n<h3>Step 3: Paste the Export Syntax Here<\/h3>\n<p>Once you visit the end of the file, paste the export syntax.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">export PATH=\"\/Directory1:$PATH\"<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1003\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax.png\" alt=\"paste export syntax at end\" width=\"800\" height=\"210\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax-300x79.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax-150x39.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax-768x202.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax-100x26.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-syntax-700x184.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<h3>Step 4: Store and Leave.<\/h3>\n<p>Now, just Save the task you have done and Exit.<\/p>\n<h3>Step 5: Run the Script Now.<\/h3>\n<p>Run the script or restart the System to put the modifications into effect.<\/p>\n<h3>Step 6: In this Step, Confirm the Modifications<\/h3>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1004\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo.png\" alt=\"run echo path command to verify changes\" width=\"800\" height=\"171\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo-768x164.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/verify-changes-run-echo-700x150.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>Run Echo to Confirm the Modifications. A directory for the current user only can be added by editing the .bashrc file. Edit the .profile file to add the path to the PATH for all users:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1005\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file.png\" alt=\"add the directory to PATH for all users\" width=\"800\" height=\"191\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file-300x72.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file-150x36.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file-768x183.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file-100x24.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/add-directory-profile-file-700x167.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<h2>Linux: Removing a Directory from the PATH<\/h2>\n<p>To remove a directory from PATH, there isn&#8217;t a single command. Nevertheless, several options make it possible.<\/p>\n<h3>Medium 1: Leave the Terminal<\/h3>\n<p>When a directory is added temporarily to PATH, removing it is easy. The directory can only be added to the terminal during the active session. When the current session expires, the directory is automatically deleted from PATH.<\/p>\n<p>Just close the terminal or you can restart the system, If you want to remove a temporary directory from PATH.<\/p>\n<h3>Medium 2. Modify Configuration Files<\/h3>\n<p>Use the same procedure to get rid of the directory export string if it was added to the .bashrc or .profile file. In a text editor, open the file, go to the end of the file, and delete the directory.<\/p>\n<h3>Medium 3: Going for String Replacement Concept<\/h3>\n<p>Use string substitution to remove a directory from the PATH:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">export PATH=${PATH\/'\/Directory1'\/}<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1006\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path.png\" alt=\"remove directory from path use string replacement\" width=\"800\" height=\"190\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path-300x71.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path-150x36.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path-768x182.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path-100x24.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/string-replacement-remove-directory-path-700x166.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>Only the string from the current session is deleted by the command.<\/p>\n<h3>Medium 4: Utilize a One-Liner in this Method<\/h3>\n<p>To remove a directory from PATH, you can also use the commands tr, grep, and paste. For illustration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">export PATH=\"$( echo $PATH| tr : '\\n' |grep -v Directory1 | paste -s -d: )\"\r\n\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1007\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory.png\" alt=\"combination of tr, grep and paste to remove directory\" width=\"800\" height=\"190\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory-300x71.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory-150x36.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory-768x182.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory-100x24.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/export-path-remove-directory-700x166.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<h3>Wrapping Up on Adding a Directory to Path in Linux<\/h3>\n<p>A quick and effective technique to improve the efficiency of your computing is to add folders to your PATH environment variable. You can make sure that your preferred tools and scripts are easily accessible from the command line by following the instructions provided in this article.<\/p>\n<p>Mastering PATH configuration is a useful skill that will ultimately save you time and effort, whether you&#8217;re customizing your development environment or improving the usability of your system. So go ahead and expand your PATH to enhance your command-line experience; it&#8217;s a little modification that can have a significant impact.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For anyone working with command-line tools and scripts, managing the system&#8217;s PATH (short for &#8220;Path to All the Executables&#8221;) is an essential chore. You may&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1025,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/992"}],"collection":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/comments?post=992"}],"version-history":[{"count":7,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/992\/revisions"}],"predecessor-version":[{"id":2485,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/992\/revisions\/2485"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media\/1025"}],"wp:attachment":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media?parent=992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/categories?post=992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/tags?post=992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}