Delete from path linux

How to remove a directory path from PATH permanently?

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=$ PATH=$ 

Источник

How to remove a path from system path(`$PATH`) using terminal commands?

I want to remove /home/avinash/Desktop/raj from system path variable by using command(like export command does on adding path).

9 Answers 9

In your current shell (your current session of gnome-terminal) you can do this using:

deletes shortest match of $substring from back of $string .

Running export PATH=$PATH:/. doesn’t set your PATH system-wide. It’s just a shell variable. Start a new shell and BOOM, it’s gone. Obviously if you’ve added that to ~/.bashrc (or another environment bootstrap file) you’ll have to revert that change but it doesn’t sound like your problem here.

If you’re desperate not to start a new shell, you could set it by removing it manually, with:

export PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 

If you put the export statement in any shell initiation file like ~/.bashrc , you can use the following commands in terminal,

#remove the export statement from the file. sed -i 's#export PATH=$PATH:/home/avinash/Desktop/raj##g' ~/.bashrc #source ~/.bashrc . ~/.bashrc 

It will remove the folder from path.

If you have exported the path from a terminal

The folder will be in path as long as you are in that shell. To overwrite the path you have to assign new path. As oli already mentioned in the other answer.

You can use the following command to set old path

This is Substring Replacement,

If you want use it as a command, here is a little script:

#!/bin/bash # This script removes folder from PATH variable # Folders to remove reading as arguments if [ $# -lt 1 ]; then echo "You should give at least one argument" echo "For example" echo "$0 /usr/local/bin" else FOLDERS_TO_REMOVE=`echo $@ | sed 's/ /|/g'` echo "You actually PATH variable is:" echo $PATH echo "###" PATH=$( echo $ | tr -s ":" "\n" | grep -vwE "($)" | tr -s "\n" ":" | sed "s/:$//" ) echo "Now you need to run" echo "export PATH=$PATH" fi 

Name it unexport , and add it to your PATH.

Читайте также:  Команды для виртуальной машины линукс

unexport /usr/local/bin /bin /sbin

This script does not change your actually PATH . If you want script to do it, you should change last line. Substitute echo «export PATH=$PATH» to export PATH=$PATH

export PATH="$( echo $PATH| tr : '\n' |grep -v raj | paste -s -d: )" 
  1. separate each dir in your PATH by line using tr
  2. remove what you don’t want (path matching «raj») using grep -v , and
  3. collapse back into a long «:» delimited string using paste .

this probably wont work well if any dir in PATH has : or a new line

if you find yourself doing this a lot, consider making it a function and saving in your shell profile (e.g. .bashrc , .zshrc )

# use like: rminpath "raj" rminpath()

export PATH=$PATH:/home/avinash/Desktop/raj (Here you have added the file to the path variable.)

echo $PATH
If you execute this command, This is the output:

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/avinash/Desktop/raj 

How do you remove it from the path? just execute

It is a concept of string replacement.

The shell builtin command export Doesn’t determine whether or not a variable is set, it determines if the variable and its value are also set in sub-processes.

Changing a variable, exported or not, does not affect the parent process or above, so changing it in a script has no effect unless that script is run with source .

will trim off the last component of the PATH variable. Its export status is unaffected.

declare -x PATH=»/desired/path/you_want» if I’m not mistaken that itself will update what you want and it’s completely at your behest what it will be. [Make sure you run as root AND as the desired user for which you want this change to affect] To be double sure, run ENV afterwards to make sure that the $PATH was effectively altered as you want. (This is usable for altering any of the output you will get when you run ‘/usr/bin/env’. Although I dont know whether or not it is permanent. In that respect do as stated above and alter ~/.bashrc for it to stick)

I simply use sed, like this

PATH=$(sed "s/\/home\/avinash\/Desktop\/raj//"  

A little explain how this will work: First we define the variable value setting PATH=, then we must say to use the result value of next command as the value of this variable just using $(command), after came the SED command sed "s///", the / will be the separators for this command, by default we use / but you can set anything like - or _. The first s is for replace, then came the first separator, after came the expression you look for, then another separator, then the expression you want to replace the first one (that you look for), then another separator (the last one) and you can use some argument like g, for a global replace. The \ is a scape character that used for make sed understand that next / is a literal and not the separator. The is for drop the variable at the command sed.

Источник

Removing a directory from PATH

I'm trying to compile wxWidgets using MingW, and I have cygwin in my path, which seems to conflict. So I would like to remove /d/Programme/cygwin/bin from the PATH variable and I wonder if there is some elegant way to do this. The naive approach would be to echo it to a file, remove it manually and source it, but I bet there is better approach to this.

18 Answers 18

There are no standard tools to "edit" the value of $PATH (i.e. "add folder only when it doesn't already exists" or "remove this folder"). You can execute:

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

that would be for the current session, if you want to change permanently add it to any .bashrc, bash.bashrc, /etc/profile - whatever fits your system and user needs. However if you're using BASH, you can also do the following if, let's say, you want to remove the directory /home/wrong/dir/ from your PATH variable, assuming it's at the end:

PATH=$(echo "$PATH" | sed -e 's/:\/home\/wrong\/dir$//') 

So in your case you may use

PATH=$(echo "$PATH" | sed -e 's/:\/d\/Programme\/cygwin\/bin$//') 

If the path in question is at the beginning of the PATH variable, you need to match the colon at the end. This is an annoying caveat which complicates easy generic manipulations of PATH variables.

When dealing with so many slashes I prefer to change the regex delimiter / with something like | : PATH=$(echo "$PATH" | sed -e 's|:/d/Programme/cygwin/bin$||') to prevent all the escaping.

This answer explains how to make the pattern more flexible so it works regardless of whether it's at the end of the path or not: superuser.com/a/1117805 This would yield PATH=$(echo "$PATH" | sed -e 's/:\/d\/Programme\/cygwin\/bin\(:\|$\)//')

Good answer. for those like me that have fat fingers, I would recommend creating a "PRACTICEPATH" to avoid potential grief. Other than that, I think the answer might be improved by adding a "verify" step at the end to make sure "all is well" before you have to find out "the hard way".

export PATH=`echo $PATH | tr ":" "\n" | grep -v "anaconda" | tr "\n" ":"`

That's the only solution that i can internalize and remember without having to search for this question on SO again

This adds a : at the end of PATH every time the command is used, because grep appends an EOL ( \n ) to its output. Solve this by piping the output of grep through perl -pe 'chomp if eof' .

To get rid of the trailing colon, you can also pipe the output to xargs , like so: echo $PATH | tr ":" "\n" | grep -v '/usr/local/bin' | xargs | tr ' ' ':'

directory_to_remove=/d/Programme/cygwin/bin PATH=:$PATH: PATH=$ PATH=$; PATH=$

If you don't use an intermediate variable, you need to protect the / characters in the directory to remove so that they aren't treated as the end of the search text.

The first and third line are there to arrange for every component of the search path to be surrounded by : , to avoid special-casing the first and last component. The second line removes the specified component.

A more robust version that eliminates successive directory entries from the path, such as baz:foo:foo:bar :

function path_remove < PATH=":$PATH:" PATH=$PATH=$ PATH=$ PATH=$; PATH=$ > 

The second line doubles the colons and the forth line reverts them back to single colons.

Thanks @Gilles, your answer prompted me to come up with my own solution, which only requires three manipulations of PATH rather then four. *8')

This unfortunately doesn't eliminate successive directory entries from the path, i.e. baz:foo:foo:bar removing foo becomes baz:foo:bar . This is because the colon on both sides of the pattern matches baz[:foo:]foo:bar and because the last match ended with the colon, it doesn't pick up with the next :foo: .

After considering other options presented here, and not fully understanding how some of them worked I developed my own path_remove function, which I added to my .bashrc :

function path_remove < # Delete path by parts so we can never accidentally remove sub paths if [ "$PATH" == "$1" ] ; then PATH="" ; fi PATH=$# delete any instances in the middle PATH=$ # delete any instance at the beginning PATH=$ # delete any instance in the at the end > 
function path_remove_test <( PATH=$1 path_remove $2 if [ "$PATH" != "$3" ] ; then echo "$1" - "$2" = "$PATH" != "$3" ; fi )>path_remove_test startpath:midpath:endpath startpath midpath:endpath path_remove_test startpath:midpath:endpath midpath startpath:endpath path_remove_test startpath:midpath:endpath endpath startpath:midpath path_remove_test somepath:mypath/mysubpath mypath somepath:mypath/mysubpath path_remove_test path path "" 

This ended up pretty close to Gilles' solution but wrapped up as a bash function which could be easily used on the command line.

It has the advantages that as a bash function it works like a program without needing to be a program on the path, and it doesn't require any external programs to run, just bash string manipulation.

It appears pretty robust, in particular it doesn't turn somepath:mypath/mysubpath into somepath/mysubpath :if you run path_remove mypath , which was a problem I had with my previous path_remove function.

An excellent explanation of how bash string manipulation works can be found at the Advanced Bash-Scripting Guide.

Источник

Оцените статью
Adblock
detector