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 "~"?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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 ~
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
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 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.
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