How to remove a directory path from PATH permanently?

8,741

On my vanilla install of 16.04, the PATH variable is set from .profile for login shells

First save your current PATH to a text file:

echo $PATH > currentpath.txt

I find it easiest to then open a new shell and do

sudo vi .profile

now edit or add the line to set the correct path

PATH="..."

with the path that you want inside the quotes.

The default path:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

(And it works from left to right - if an executable with the same name exists in /usr/local/sbin and /usr/bin, the version in /usr/local/sbin will be called when you type the name)

Press esc and write and quit with

:wq

If you don't know vi you could use nano to edit the .profile file. Might be easier.

Restart.

I did this so that my local bin folder was in the path for executables. Dunno why it wasn't by default, but hey.

Share:
8,741

Related videos on Youtube

Neels
Author by

Neels

Updated on September 18, 2022

Comments

  • Neels
    Neels over 1 year

    I am using Ubuntu 16.04 LTS. I am really new to Linux.

    I created a softwares directory within the Downloads dir for all the software and added it to my PATH. Then I was advised that it's better to create a bin directory in your home, instead of keeping directories such as Downloads in the PATH. So, I did it. Now, my problem, I have both the directories in the PATH and some of my tools are not running due to this reason.

    I did try some of the suggestions given in the posts here but it didn't work for me and since I am new, I am bit scared to experiment that I'll mess up every thing.

    My PATH

    $ echo $PATH 
    /home/gjjha/bin:/home/gjjha/bin:/home/gjjha/bin:/home/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/home/gjjha/bin:/home/gjjha/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin://home/gjjha/Downloads/softwares/ncbi-blast-2.5.0+:/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/home/gjjha/home/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin:/gjjha/Downloads/softwares/bowtie2-2.2.9/index:/home/gjjha/Downloads/softwares/tophat-2.1.1:/gjjha/Downloads/softwares/bowtie2-2.2.9/index:/home/gjjha/gjjha/Downloads/softwares/bowtie2-2.2.9/index:/home/gjjha/Downloads/softwares/tophat-2.1.1:/home/gjjha/bin:/home/gjjha/bin/blastdb:/home/gjjha/bin/blast+2.5:/home/gjjha/blast+2.5/bin:/home/gjjha/blast+2.5/bin:/home/gjjha/bin/ncbi-blast-2.5.0+/bin
    

    I want to remove the softwares dir and duplicates and want to keep /home/gjjha/bin since all softwares are in bin dir now.

    Commands I tried:

    PATH=echo $PATH | sed -e 's/:\/home\/wrong\/dir\/$//'
    PATH= echo $PATH | sed -e 's/:/home/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin/$//'
    PATH=${PATH/%:/home/wrong/dir//}
    PATH=${PATH%:/home/gjjha/Downloads/softwares//}
    PATH=${PATH%:/home/gjjha/Downloads/softwares/ncbi-blast-2.5.0+/bin/}
    

    I checked (even after rebooting), the softwares dir is still there.

    • Zanna
      Zanna about 7 years
      Where did you set your path? You must have edited some config file.
    • steeldriver
      steeldriver about 7 years
      Trying to edit variables on the fly with sed or parameter substitution is not the right way to approach this. Revert the changes you made then logout and login.
    • Cyrus
      Cyrus about 7 years
      Please remove this crossposting.
    • Neels
      Neels about 7 years
      @Zanna I have added the path using vim .bashrc every time.
    • Zanna
      Zanna about 7 years
      well you need to remove whatever you added to your .bashrc to set the PATH. Then it will be overidden by /etc/environment when you log out and back in. Then you can put the path you want in .profile (more appropriate than .bashrc)
    • Neels
      Neels about 7 years
      How to remove paths from .bashrc?? that is my question
    • Zanna
      Zanna about 7 years
      to replace your .bashrc with a clean one: mv ~/.bashrc ~/bashrc-bak && cp /etc/skel/.bashrc ~/.bashrc (that's all one command, ignore the break caused by the small size of the comment space)