Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In a folder owned by my user /sites/Website there is a dir named ~.

When I cd to this directory, I am redirected to ~/, my home folder.

At first I thought this was a symbolic link, but doing ls -al shows it to be ordinary. I'm assuming it is a hard link. I ran:

$ rm -r /sites/Website/~

This began to actually delete my home folder and I lost a lot of data. I stopped rm with Ctrlc. How can I get rid of this link safely?

share|improve this question
4  
In Linux, hard links to directories are not allowed (except for . and ..), so it is probably not a hardlink. What is the output of stat ~ /sites/Website/~? –  Chris Down Apr 18 '13 at 1:28
3  
You can't make hardlinks to directories. /sites/Website/~ shouldn't expand to $HOME. Did you actually change to /sites/Website and run rm -r ~? –  paraxor Apr 18 '13 at 1:29
5  
Try quoting or escaping the directory name when you use it (\~ or '~'). That way, your shell won't expand the tilde to $HOME. –  paraxor Apr 18 '13 at 1:31
    
@EvanTeitelman Even "~" does. –  Hauke Laging Apr 18 '13 at 1:33
    
check the output of mount, maybe it's the same directory mounted to two places? –  Ulrich Schwarz Apr 18 '13 at 8:57
show 1 more comment

1 Answer 1

Many shells expand a leading tilde character (~) before a slash at the start of a pathname into the absolute path of your HOME directory. You can prevent this shell expansion by quoting the tilde character or by making sure the tilde doesn't start the pathname:

$ echo "$HOME"
/home/idallen
$ echo ~
/home/idallen
$ echo "~"
~
$ echo \~
~
$ echo ./~
./~

There is nothing special about a tilde character to Unix itself, so you can create a directory named tilde, provided you hide the tilde from expansion by the shell using any of the above methods:

$ mkdir "~"
$ ls -ld '~'
drwxr-xr-x 2 idallen idallen 40 Oct 22 08:45 ~
$ ls -ld \~
drwxr-xr-x 2 idallen idallen 40 Oct 22 08:45 ~
$ ls -ld ./~
drwxr-xr-x 2 idallen idallen 40 Oct 22 08:45 ~
$ ls -ld /tmp/idallen/~
drwxr-xr-x 2 idallen idallen 40 Oct 22 08:45 /tmp/idallen/~

If you don't hide the leading tilde character from the shell, the shell expands it to be your HOME directory. Adding an echo command in front of a command line can show you what the shell does to your command line before it is executed:

$ echo cd ~
cd /home/idallen                 # would go to $HOME directory
$ echo cd ./~
cd ./~                           # would go into directory named ~

You have discovered this need for quoting the tilde, because the commands below would have very different effects, depending on your shell expanding an unquoted leading tilde:

$ echo rm -r ~       
rm -r /home/idallen              # would remove the $HOME directory
$ echo rm -r "~"
rm -r ~                          # would remove the directory named ~
$ echo rm -r ./~
rm -r ./~                        # would remove the directory named ~
$ echo rm -r /tmp/idallen/~
rm -r /tmp/idallen/~             # would remove the directory named ~

Only unquoted tildes at the start of pathnames expand to be your HOME directory.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.