Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
While attempting to routinely delete a directory and all
of its subdirectories, let us say the name of the directory is
"mydirectory.with.a.very.long.name", I first typed, not as root,
rm -r ~/
then grabbed the string "mydirectory.with.a.very.long.name" and
by mistake left a blank after / when I copied the string, so that
the command read
rm -r ~/ mydirectory.with.a.very.long.name
:-(
Of course after pressing "Enter", the computer happily proceeded to
delete my entire home directory.
Except after removing a couple of large directories and some single
files, most of which could be replaced from backups, it stopped when
it reached a write-protected file, and it asked what to do about it.
Which gave me a chance to kill the process and save most of my stuff.
I imagine that one way to protect against this happening again would
be to install a new write-protected file somewhere, and to make sure
that if rm -r ~/ is ever invoked, then it will start by trying to
remove that particular file as its first target, which will stop it.
Is this possible? Or is there an even simpler way?
The problem was that space before the directory name, so rm did wha you said so, the name after the space was ignored and rm proceded from ~/.
Just make sure what the command is before issue it to avoid such deletions.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
I would use the full path to the files or directories to be deleted to avoid any ambiguity. Also, when deleting recursively in such a situation try using ls first and using the tab to auto complete the path so that you can confirm that the path is correct.
What you should not do is create files to stop stupid commands from causing problems what you should do is stop yourself from entering stupid commands.
At first it may seem a little less convenient, but one way to avoid the dangers of leaving a stray space in the 'rm' command like you describe above is to not use cut/paste at all, instead use the filename-completion features of a shell like bash (or tcsh?). I usually only have to type the first letter or two of each directory in a long pathname, and bash writes the rest. And if a stray space (or other character) gets in there somehow the filename completion breaks right there, and tells you your mistake. And usually it's no more hassle than moving the mouse, highlighting text, etc.
-I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes
Consider as well the --preserve-root option (which I also use in my chown and chmod aliases)
1) Add an alias so when you run "rm", it automatically runs "rm -i" or "rm -I".
2) Use tab completion for finding files
3) Any time you stick a "-r" in rm, double and triple check (quintuple check if there's a "-fr") that everything is correct before pressing enter
4) Always have backups
kluto, I will repeat suicidaleggroll's point 4 above - everyone says "keep regular backups", but there's a good reason for that. They are critical, and data loss can be devastating.
If you don't want to spend money on a USB drive, then tar up your important files, encrypt them (using AES and a strong key), and ship them off to the cloud (e.g. Dropbox, Ubuntu One, whatever). I do this on a regular basis for my laptops.
Use the -i option or create an alias for rm with the -i option in your .basrc file in your home directory as suggested.
The only drawback to this is it will repeatedly ask your confirmation. If you are certain about the files and directories you wish to delete use the -f option with rm.
But be warned, make sure you check your syntax VERY CAREFULLY when using the -f option with rm because it overrides the interactive mode.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.