Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a folder called ~ [duplicate]

Tags:

bash

rm

I had bug in my bash script that created folder called

~

Anyway that lead me to another problem which is exscuting the following command:

rm -fr ~

ooopsss removed my home folder...

so my question is:

why rm works on the alias and not on the actual folder that is listed where I am?

You can reproduce it, by the following:

cd /tmp/
mkdir "~"
which rm ~

now you will see that the output of which rm ~ is pointing to the actual home folder and not the the ~ which was created in tmp

like image 456
talsibony Avatar asked Feb 23 '26 08:02

talsibony


2 Answers

The ~ is expanded by the shell to the home directory name before rm is called, the rm program is unaware of it.

Expansion of magic characters like this can be suppressed by enclosing them in quotes:

$ echo ~
/Users/fredbloggs
$ echo '~'
~
like image 151
cdarke Avatar answered Feb 25 '26 21:02

cdarke


rm -R ./~

That will make it look for ~ in the current directory. If you run

rm -rf ~

You are removing the original ~ so you need to indirect with ./~

This happens because of the expansion of the special character

like image 35
Andre Motta Avatar answered Feb 25 '26 22:02

Andre Motta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!