gedit creates hidden backup files ending with '~'. I wanted to do a recursive cleanup of my directory tree.
The command rm *~
will delete all local files ending with '~'
I thought rm -r *~ .
would delete all files in the whole tree, but I typo-ed rm -r ~
.
There was a message some directory could not be deleted and I quit the command. The question is: What have I been deleting?
I did notice that my Filezilla configuration was gone. Does this command delete all hidden directories from the home dir?
You have deleted (almost) all of your home directory!
rm -r something
recursively deletes files and directories in/under something; it will only fail on items it does not have permission to delete, either because they are owned by a different user (e.g.root
), or because they are in use (e.g. a GVFS mount).If you did not add
-f
(as in,rm -rf something
), you would get something like:At this point you can abort and you then likely realise that you executed the wrong command.
Suggestions from the community on how to avoid such accidents
For a few files, use the
-i
option to get a confirmation on removing files or directories:If removing from a current directory, prefix it by a
./
to avoid mistakingly removing a different location.:The above was for demonstrating the use of
./
. If there is a subdirectory, you can of course userm -v something/*~
as well. Beware: do not think thatrm ./.*
removes all hidden files in the current directory. See this Serverfault question for workarounds on hidden files globbing.Move rather than delete: Rather than deleting files recursively in the first place I got used to move the files I want to get rid of somewhere else before I delete them finally. If something went wrong its easy to restore.
Use
find
(possibly withgrep
) to preview if you want to recursively delete selected files. You should try to make this a rare occurrence, but if you have to...find
with no arguments recursively lists all files/directories under the current one. You should try to look upman find
on how to make it selective (a treasure trove), but if you don't want to bother, you can just use the familiargrep
to filter for the files you want to delete.find | grep example
, which gives me these 20 files. That looks good, so I now go and delete those exact files, along with therm -v
verbose output previously mentioned, viaxargs
: which gives me this output. Such previewing prevents problems where say, you make a typo and typesample
instead ofexample
.The above solution should not be used if you may have filenames containing spaces, tab characters, newlines, or quotes (
"
or'
), as this will leadxargs
to either fail, or feedrm
with incomplete filenames, which in worst case could end up removing the wrong files. The safe way to do the above is to do it all withfind
's operators and actions.The following will show all files that contain the word
example
If that list is the files you want to remove, use the
-exec
action to pass the files torm
.See Using Find for more help with using
find
.Yes. An unquoted tilde character as a separate argument gets expanded to your homedir. So
rm -r ~
got expanded torm -r /home/yourusename
. So you told rm to recursively delete all files in your homedir.See http://www.gnu.org/software/bash/manual/bashref.html#Tilde-Expansion for more about how tilde expansion works.