I need to delete a "~" folder in my home directory.

I realize now that rm -R ~ is a bad choice.

Can I safely use rm -R "~"?

  • 2
    I think it is, but to be safe, you could cd in, verify the contents, rm everything inside, then cd .. And rmdir the now-empty folder. This assumes rmdir will refuse to remove a non-empty dir, which I assume your home directory is. – Kalvin Lee Apr 27 '16 at 18:52
  • 1
    There's a question like this for every funky char... – don_crissti Apr 27 '16 at 18:53
  • 1
    @don_crissti yes, but I really wanted to be sure so I don't delete my entire home directory. ^_^ – Adam Thompson Apr 27 '16 at 19:00
  • 1
    @AdamThompson Just test it out first: echo rm ~ vs echo rm "~" – user253751 Apr 28 '16 at 2:39

In theory yes. In practice usually also yes. If you're calling a shell script or alias that does something weird, then maybe no.

You could use echo to see what a particular command would be expanded to by the shell:

$ echo rm -R ~
rm -R /home/frostschutz
$ echo rm -R "~"
rm -R ~

Note that echo removes the "" so you should not copy-paste what it prints. It just shows that if you give "~", the command literally sees ~ and not the expanded /home/frostschutz path.

If you have any doubt about any command, how about starting out with something that is less lethal if it should go wrong? In your case you could start out with renaming instead of deleting it outright.

$ mv "~" delete-me
$ ls delete-me
# if everything is in order
$ rm -R delete-me

For confusing file names that normally shouldn't even exist (such as ~ and other names starting with ~ or , or containing newlines, etc.), it's better to be safe than sorry.

Also consider using tab completion (type ls ~<TAB><TAB><TAB>), most shells try their best to take care of you, this also helps avoid mistyping regular filenames.

| improve this answer | |
  • 2
    The second advice (mv "~" delete-me etc.) is excellent. This is really helping to avoid the stress of a mistyped/misunderstood command. – WoJ Sep 12 '18 at 13:06
  • Also found the mv "~" delete-me advice really useful. – misteraidan Aug 19 '19 at 7:39

As Kalvin Lee mentioned, you can cd to the directory and remove its contents, then use rmdir to remove the directory. I recommend this over the rm -R approach because you're less likely to fat-finger the command and blow away your home directory.

Generally, you can put things that you don't want the shell to expand in single quotes. This will remove an empty directory named ~:

rmdir '~'
| improve this answer | |
  • 1
    Not rmdir. OP is clear that this directory isn't empty yet, which is why he's using rm -R. – Warren Young Apr 27 '16 at 18:58

In addition to frostschutz's double quotes method, and Andy's simple quote one, there are also the shorter:

rm -r \~

and the relative path one:

rm -rf ./~
| improve this answer | |

I know this has been answered already, but I always use mc - Midnight Commander to delete awkward files that I am too afraid in attempting to delete. It is a "GUI" like interface where you simply highlight the file you want to delete, and delete it.

| improve this answer | |

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