safer alternative then rm -rf to remove non-empty directory?
Hi,
I wonder is there any safer alternative to remove non-empty directory then rm -rf dirname/* If one mistype dirname, it could end up system wide wipping. Can anyone give better alternative? thanks |
Always specify the fully qualified path, eg
Code:
rm -rf /home/username/dirname |
You are right that it's possible to do serious harm to your system. But apart from having the system confirm every single action it takes (with `rm -ri` for example) at some point you have to trust yourself and live with the occasional mistake.
As long as you're regularly backing up your system config files and your data you'll be able to recover (mostly ;) ) from the mistake... |
Or you could use a filemanager. You have Midnight Commander for text environments, and you have things like Nautilus in Gnome. I find these graphic applications to be more dangerous than a command line but maybe that's just the way my mind works.
|
Safest method to use a potentially dangerous command: put it in a shell script, then run the script. Any typos, such as hitting the enter key at the wrong time while composing the command, are trapped in the editor, and not executed. That gives you time to read and verity the command is entered correctly before you run the script.
Or do as gilead suggested; take your chances. You can always restore from backup (you do backup, don't you?). |
well, in script is ok. But what if down to the track directory name got change? Ever try to do rm -rf invalid_directory/* ?
I am thinking about to write a customized function to do rm -rf where it only execute when path/directory is existed. Or alternatively, maybe su to non-root user first before execution? Restore from backup is doable but pain in ass. Just how much time its going to take....plus it a recovery method not a preventative apporach. |
One solution:
Create a wrapper script for rm that actually just moves the files to any arbitrary directory, then have a weekly cron task to empty the directory. This way if you screw up you have a week to restore the files. If you get really creative you can set it up to run every night, but only delete those files older than 'n' days old. |
Mmm sneaky bulliver ;)
I still don't understand why people use the -f flag with rm... If you're concerned about removing important stuff then why 'force' it? Drop the -f and any read only files etc will raise a warning and ask you to proceed. Code:
rm -r /home/cam/dirname |
Quote:
Although that double-entendre was a little sneaky ;) |
Not much good getting used to wrappers - what happens when you go use another machine and expect the same safety net ???.
Experience is a great teacher - screw up and you remember the pain of the situation. Might even do it twice, but that'd (hopefully) be the limit. |
All times are GMT -5. The time now is 05:39 AM. |