Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This 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.


LinkBack Search this Thread
Old 01-15-2006, 01:22 PM   #1
Registered: Jun 2005
Distribution: debian, ubuntu, redhat,knoppix
Posts: 194

Rep: Reputation: 31
safer alternative then rm -rf to remove non-empty directory?


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?

Old 01-15-2006, 01:38 PM   #2
LQ Veteran
Registered: Mar 2003
Location: Milwaukee, WI
Distribution: Mint
Posts: 6,642

Rep: Reputation: 69
Always specify the fully qualified path, eg
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)
Old 01-15-2006, 01:42 PM   #3
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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...
Old 01-15-2006, 01:46 PM   #4
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
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.
Old 01-15-2006, 01:59 PM   #5
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,696

Rep: Reputation: 286Reputation: 286Reputation: 286
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?).
Old 01-17-2006, 06:46 PM   #6
Registered: Jun 2005
Distribution: debian, ubuntu, redhat,knoppix
Posts: 194

Original Poster
Rep: Reputation: 31
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 it a recovery method not a preventative apporach.
Old 01-17-2006, 07:05 PM   #7
Senior Member
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 64
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.
Old 01-17-2006, 07:29 PM   #8
Senior Member
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 56
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.
rm -r /home/cam/dirname
Old 01-17-2006, 07:40 PM   #9
Senior Member
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 64
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
Old 01-17-2006, 08:24 PM   #10
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,801

Rep: Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
rm: cannot remove directory `ff': Directory not empty blancs Linux - General 17 08-29-2011 01:47 PM
remove a non empty directory in shell acidjuice Slackware 10 06-28-2011 12:02 PM
How do you remove a non empty directory aaronruss Linux - Newbie 3 06-05-2004 08:26 PM
RMDIR - how to remove a non-empty directory?? jacksmash Linux - Newbie 15 11-23-2003 12:51 PM
How to remove a non-empty dictory? Xiangbuilder Linux - Newbie 6 09-03-2003 03:40 PM

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration