46

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?

  • Try to delete it by i-node. – gronostaj Jun 19 '13 at 5:25
55

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.

  • This worked. Perhaps the the solution by rici is good too, but I didn't try. Thanks a lot! – scrrr Jun 19 '13 at 7:46
  • Yes, rici's answer is good too. And I think it's better because it's simpler. – Andrey Regentov Jun 20 '13 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 '13 at 23:21
  • Don't worry. Always glad when somebody deems my answers worthy of their time :) – Squeezy Jun 29 '13 at 22:19
  • This answer didn't work for me. Ubuntu 14.04 LTS – KhoPhi Mar 1 '16 at 2:43
46

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.

  • 17
    also, going to the folder and rm ./~, for the same reason you said – Carlos Campderrós Jun 19 '13 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 '18 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 '18 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 '18 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.

  • 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 '13 at 5:08
  • @scrrr What's your shell? – Daniel Beck Jun 19 '13 at 7:49
  • bash. But problem solved the inode way. – scrrr Jun 19 '13 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. – Isaac Rabinovitch Jun 25 '13 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 '14 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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