Linux: Change the PATH [ Add New Directory ]

To add a new directory to the PATH environment variable in Linux, you can follow these steps:

  1. Open a terminal window.
  2. Check your current PATH by running the following command:
    echo $PATH

    This will display a list of directories separated by colons. These directories are searched in order when you type a command in the terminal.

  3. Create a new directory that you want to add to the PATH. For example, you might create a directory called bin in your home directory:
    mkdir ~/bin
  4. Add the new directory to your PATH by editing your shell startup file. The exact file you need to edit will depend on which shell you’re using. For example, if you’re using the Bash shell, you would edit your ~/.bashrc file:
    nano ~/.bashrc

    Add the following line at the end of the file:

    export PATH=$PATH:~/bin

    This appends the new directory to the existing PATH variable, separated by a colon.

  5. Save and close the file. In nano, you can do this by pressing Ctrl+X, then Y, then Enter.
  6. Reload your shell startup file by running the following command:
    source ~/.bashrc

    This will apply the changes you made to your PATH environment variable.

  7. Verify that the new directory has been added to your PATH by running the following command:
    echo $PATH

    This should display the original directories in your PATH, followed by the new directory you added.

You can now place executable files in your new directory (~/bin in this example) and run them from any location in the terminal.

Leave a Comment