Safe to delete a folder named "~"

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?

Doug Miller

Posted 2014-01-30T00:07:49.027

Reputation: 141

Question was closed 2014-01-30T20:23:34.223

Have you tried to delete the hard link? You need to determine if your dealing with a hard link or not. – Ramhound – 2014-01-30T00:13:27.920

@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 – 2014-01-30T00:14:44.503

@bob didn't seem like one after 30 seconds of research on hard links /-) – Ramhound – 2014-01-30T00:17:36.547

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 – 2014-01-30T00:21:00.093

Its not a hardlink: inode from /home/me 20185089, from other dir 28579771 – Doug Miller – 2014-01-30T01:31:45.890

Answers

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.

ecube

Posted 2014-01-30T00:07:49.027

Reputation: 558

One extra degree of security could be obtained by using the full path, e.g. rm -Ri /path/to/the/tilde/\~ – ernie – 2014-01-30T01:16:41.427

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. – Doug Miller – 2014-01-30T01:33:23.067

@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 – 2014-01-30T14:02:07.453

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/~

Adaephon

Posted 2014-01-30T00:07:49.027

Reputation: 3 864