safer alternative then rm -rf to remove non-empty directory?
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.
If you make typos, there's less chance that you could accidentally delete the wrong thing (probably because the misspelled path/directory doesn't exist and so the command will fail)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.