LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


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

Rep: Reputation: 31
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
 
Old 01-15-2006, 01:38 PM   #2
J.W.
LQ Veteran
 
Registered: Mar 2003
Location: Boise, ID
Distribution: Mint
Posts: 6,642

Rep: Reputation: 87
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)
 
Old 01-15-2006, 01:42 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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
taiwf
Member
 
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 take....plus it a recovery method not a preventative apporach.
 
Old 01-17-2006, 07:05 PM   #7
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
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
 
Old 01-17-2006, 07:40 PM   #9
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

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

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


Reply



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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:50 PM.

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