50

I just noticed that I have a file called ~ in my ~-directory.

$ ls -la ~
...
-rw-r-----  1 x1 x1  733962240 Mar  1 17:55 ~
...

Any idea how I can mv or rm it?

1
  • Try to delete it by i-node.
    – gronostaj
    Jun 19, 2013 at 5:25

4 Answers 4

58

The pretty much ultimate solution when it comes to files that can't be deleted by normal means:

ls -il 

The first column will show the inode number of the files.

find . -inum [inode-number] -exec rm -i {} \;

This will delete the file with the specified inode-number after verification.

6
  • This worked. Perhaps the the solution by rici is good too, but I didn't try. Thanks a lot!
    – scrrr
    Jun 19, 2013 at 7:46
  • Yes, rici's answer is good too. And I think it's better because it's simpler. Jun 20, 2013 at 4:51
  • nice, +1. I changed your answer to use -delete instead of -exec, then realized that you were using rm -i which is a good idea so I rolled back. Sorry.
    – terdon
    Jun 26, 2013 at 23:21
  • Don't worry. Always glad when somebody deems my answers worthy of their time :)
    – Squeezy
    Jun 29, 2013 at 22:19
  • This answer didn't work for me. Ubuntu 14.04 LTS
    – KhoPhi
    Mar 1, 2016 at 2:43
52

You should be able to refer to that file as ~/~ (without quotes) because tilde-expansion only applies the the tilde (~) at the very beginning of the word.

4
  • 20
    also, going to the folder and rm ./~, for the same reason you said Jun 19, 2013 at 7:10
  • @CarlosCampderrós consider making that an answer. I would deem it as the "best" and most straightforward solution, compared to other answers since ./~ literally means "a file or folder named ~ in the current directory". There are no hacks like escape characters or inode references.
    – ADTC
    Jan 11, 2018 at 17:36
  • @ADTC: in defence of my answer, it does not require changing the CWD, whereas using ./~ does. After five years, my memory is a little fuzzy, but I think that is why I suggested ~/~ rather than the more normal ./~: The question specifically refers to "a file named ~ in [OP's] home directory"; not "in the current directory".
    – rici
    Jan 11, 2018 at 18:20
  • @rici That makes sense, and in the context of the question it would be appropriate. But I do like to see answers that are general, or speak generally first before expounding more to the question's specific case. That's my defense :D
    – ADTC
    Jan 12, 2018 at 0:31
20

Quote it (rm '~') or escape it (rm \~).


It's always either of those (also for e.g. $), or add -- to prevent the file name from being interpreted as argument: rm -- -i removes the file named -i; also useful for rm -- * when you want to delete all files in the current directory: No accidental rm -f * just because a file is named like that.

7
  • Hm, no that doesn't seem to work.. I tried the single quotes, the escaping and backticks.. he doesn't pick up the file. Says either it doesn't exist or interprets ~ as the home-directory..
    – scrrr
    Jun 19, 2013 at 5:08
  • @scrrr What's your shell?
    – Daniel Beck
    Jun 19, 2013 at 7:49
  • bash. But problem solved the inode way.
    – scrrr
    Jun 19, 2013 at 9:06
  • @scrrr Glad your problem is solved, but now I want to know why bash escapes don't work the same way on your system as it does on mine. Jun 25, 2013 at 21:28
  • 1
    Careful. -- only stops the following arguments being interpreted as parameters - it doesn't prevent the shell from performing its expansion (e.g. *), which is what's happening here. (I know you suggested it as an alternative for other situations, but a warning would be good.)
    – Bob
    Jan 30, 2014 at 0:17
1

Just to be safe I tried this in mac catalina:

mv '~' '~_bkp'

you can change directory to check the contents

cd '~_bkp' ls

If its empty simply 'remove'

rm -rf '~_bkp'

'rmdir' can be used too to remove the empty directory

You must log in to answer this question.

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