1

I program I was using to out put some files apparently did not understand the short cut "~" (directory) very well and has done some trickery that has me confused.

Rather than put the files where I would expect them (/home/me) it has a made a folder in it's subdirectory called "~". (someprogram/~)

drwxr-xr-x 3 me users 4096 Jan 30 09:34 ~

If I cd into it I get back to /home/me

Is this just a hardlink? I imagine if I try to delete it will will delete my home folder. How can I safely remove it and not delete everything?

5
  • Have you tried to delete the hard link? You need to determine if your dealing with a hard link or not.
    – Ramhound
    Jan 30, 2014 at 0:13
  • @Ramhound It's not a hardlink. Well, shouldn't be (Hyposaurus, make sure you compare the inode of this one and the actual home dir to make sure) - it's tilde expansion in bash.
    – Bob
    Jan 30, 2014 at 0:14
  • @bob didn't seem like one after 30 seconds of research on hard links /-)
    – Ramhound
    Jan 30, 2014 at 0:17
  • 1
    @Ramhound A hardlink is basically two files pointing to the same data on disk (inode). Deleting one 'file' of a hardlink removes just the pointer, while modifying the data (or truncating it) will overwrite the actual data and 'change both files'. Unfortunately, if it's not actually a hardlink, and is a shell expansion (as in this case), deleting it could very well lead to undesired loss of data. Always use rm -i when uncertain, and try to ask if you can (on that note - thanks for asking before, rather than delete and ask about recovery!).
    – Bob
    Jan 30, 2014 at 0:21
  • Its not a hardlink: inode from /home/me 20185089, from other dir 28579771 Jan 30, 2014 at 1:31

2 Answers 2

2

It shouldn't be a hard link, because as far as I know, folders can't be hardlinked except by root, and even then it might be impossible due to system restrictions. Just to make sure, try this (don't confuse \ with /):

unlink \~

(you have to escape the ~, or the shell will interpret it as "unlink $HOME").

Note that unlink will remove it, even if it's a regular file (but not if it's a directory)

If it's a directory, just do this:

rm -Ri \~

Again, escape the tilde. YOu want to do rm -Ri so it prompts you before every removal, in case you forget to escape the tilde and you accidentally remove your homedirectory.

3
  • One extra degree of security could be obtained by using the full path, e.g. rm -Ri /path/to/the/tilde/\~
    – ernie
    Jan 30, 2014 at 1:16
  • It is not a hard link and it is a directory and 'rm -Ri /home/me/Code/junk/\~' wants to descend into my home directory. Nothing is deleted but it looks like neither of these commands will help me. Jan 30, 2014 at 1:33
  • @Hyposaurus that makes no sense. There is absolutely no connection between /whatever/path/~ and ~ as $HOME. The shell will only interpret ~ as $HOME if it is the first character of a path and it should never expand \~. The command should just ask you rm: remove directory ‘/home/me/Code/junk/~’? and you should answer yes and that's all. If it is descending into your $HOME, there is something else going on. If so, post the output of ls -id $HOME /home/me/Code/junk/~'
    – terdon
    Jan 30, 2014 at 14:02
1

Tilde expansion is only done by the shell, when a word is starting with ~. Inside or at the end of a word there is no special treatment of ~. This is the same for a lot of shells (bash, zsh, pdksh, tcsh, dash, jsh, sash). There are also some shells that do not know tilde expansion (sh (Bourne shell), ash)

So if you escape it it or use a path instead of just ~, nothing will be expanded. It does not matter if it is relative or absolute path, there just has to be something before ~.

rm -Ri \~
rm -Ri '~'
rm -Ri "~"
rm -Ri ./~
rm -Ri /full/path/~

Not the answer you're looking for? Browse other questions tagged .