Linux path delete or deleting

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

Читайте также:  Хостинг панель для linux

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.

Источник

How to delete or remove a directory on Linux

In this tutorial, you will learn the different methods of how to delete or remove a directory on a Unix or Linux filesystem.

There are a few utilities provided by Linux for deleting things on a Linux filesystem. Each simplistic and with their own strengths, but extremely effective. You will learn about the rm and the rmdir commands and their differences.

You will also learn how to securely wipe data from a Linux filesystem using the rm command. An important and crucial understanding when dealing with Personal Identifiable Information (PII) or passwords.

How to use the rmdir command

The rmdir command removes a directory or list of directories from the file system, provided that they are empty. The command is very simplistic and does not offer much in the way of features.

Deleting a single, empty directory.

To delete a single empty directory rmdir enter the directory path as an argument. For example, to delete a directory /tmp/logs , you would use the following command.

Deleting Multiple Empty Directories with rmdir

A list of directories can be given to the rmdir command. Each directory will be removed, provided it is empty.

To delete multiple directory you will need to use the -p flag. When set, each directory argument is treated as a pathname of which all components will be removed.

For example, to delete directories /tmp/logs1 , /tmp/logs2 , and /tmp/downloads , you would run the following rmdir command.

rmdir -p /tmp/logs1 /tmp/logs2 /tmp/downloads

Deleting non-empty directories with rm

The rm command is typically used for removing non-directory type files. However, it does support removing directories and it is the most common method for removing them.

Unlike the rmdir command, the rm command will remove empty and non-empty directories. If a directory is not empty, its contents will be deleted along with the directory. This includes nested directories.

Removing directories and other file types with rm

To remove directories with the rm command, you can use -d flag. This instructs rm to include directories when deleting filesystem objects, which are excluded by default.

Deleting nested directory hierarchy with rm

The rm command can also delete nested directories. In order to delete a directory hierarchy on a Linux filesystem, you use the -r or -R flags.

The rm command will prompt you to confirm the nested directory deletion, as this can be a very destructive action.

Forcefully delete nested directories with the rm command

The most commonly used way of deleting directories and nested directories on a Linux file system is to do so forcefully. This a very dangerous way of deleting an entire directory hierarchy, as there is no confirmation prompt.

All files and directories in the path given will be removed from the filesystem, provided the user has permission to those files and directories. Use this command with extremely caution.

To forcefully remove all files and directories, you use the -r and the -f flags. Flags can be combined with a single hyphen to simply the command.

For example, to delete all files and directories under the path /tmp/app , you would use the following command.

Secure privacy deletes with the rm command

A typical file deletion on nearly all filesystems only deletes the file’s entry in the filesystem database. The bytes will remain on disk until they are eventually overwritten.

When dealing with highly sensitive data, a simple file or directory deletion will not securely remove the data. The rm command is capable of overwriting data on file deletion, and it does this by written a pattern of 0xff , 0x00 , and 0xff over the files.

This approach effectively makes it impossible to recover the data.

To securely delete files and directories from a Linux filesystem, use the -P flag.

Conclusion

When deleting a file from a Linux or Unux filesystem, there are several ways the task can be accomplished. You can use either the rmdir command or the rm command, depending on your needs.

The rm command is the most common method for deleting files, as it is the only methods to delete non-empty directories. However, it can also be the most dangerous tool to use when used inappropriately.

We’ve also learned that the rm command provides a method for secure file and directory deletion. Anytime you are handling sensitive data, such as passwords or Personal Identifiable Information (PII), it should also be deleted in a secure way to protect individual privacy and to prevent data leaks.

Источник

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