LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   safer alternative then rm -rf to remove non-empty directory? (https://www.linuxquestions.org/questions/linux-general-1/safer-alternative-then-rm-rf-to-remove-non-empty-directory-403668/)

taiwf 01-15-2006 01:22 PM

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

J.W. 01-15-2006 01:38 PM

Always specify the fully qualified path, eg
Code:

rm -rf /home/username/dirname
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)

gilead 01-15-2006 01:42 PM

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...

stress_junkie 01-15-2006 01:46 PM

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.

bigrigdriver 01-15-2006 01:59 PM

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?).

taiwf 01-17-2006 06:46 PM

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.

bulliver 01-17-2006 07:05 PM

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.

cs-cam 01-17-2006 07:29 PM

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

bulliver 01-17-2006 07:40 PM

Quote:

Mmm sneaky bulliver
Not really. This is what MS has been doing with their 'trash' since the beginning...
Although that double-entendre was a little sneaky ;)

syg00 01-17-2006 08:24 PM

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.