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 02-01-2014, 10:16 PM   #1
kluto
LQ Newbie
 
Registered: Feb 2014
Posts: 3

Rep: Reputation: Disabled
How best to avoid deleting all files


While attempting to routinely delete a directory and all
of its subdirectories, let us say the name of the directory is
"mydirectory.with.a.very.long.name", I first typed, not as root,
rm -r ~/
then grabbed the string "mydirectory.with.a.very.long.name" and
by mistake left a blank after / when I copied the string, so that
the command read
rm -r ~/ mydirectory.with.a.very.long.name
:-(
Of course after pressing "Enter", the computer happily proceeded to
delete my entire home directory.
Except after removing a couple of large directories and some single
files, most of which could be replaced from backups, it stopped when
it reached a write-protected file, and it asked what to do about it.
Which gave me a chance to kill the process and save most of my stuff.

I imagine that one way to protect against this happening again would
be to install a new write-protected file somewhere, and to make sure
that if rm -r ~/ is ever invoked, then it will start by trying to
remove that particular file as its first target, which will stop it.

Is this possible? Or is there an even simpler way?

Thanks in advance!

--kluto
 
Old 02-01-2014, 10:22 PM   #2
ukiuki
Senior Member
 
Registered: May 2010
Location: Planet Earth
Distribution: Debian
Posts: 1,030

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
Welcome to LQ !

The problem was that space before the directory name, so rm did wha you said so, the name after the space was ignored and rm proceded from ~/.
Just make sure what the command is before issue it to avoid such deletions.

Cheers
 
Old 02-01-2014, 10:31 PM   #3
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
I would use the full path to the files or directories to be deleted to avoid any ambiguity. Also, when deleting recursively in such a situation try using ls first and using the tab to auto complete the path so that you can confirm that the path is correct.
What you should not do is create files to stop stupid commands from causing problems what you should do is stop yourself from entering stupid commands.
 
Old 02-02-2014, 01:09 PM   #4
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 374
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
One option is to use the interactive option.

# echo 'alias rm="rm -i"' >>~/.bash_profile
# . ./.bash_profile
# rm -r ~ /.foo/bar
rm: descend into directory '/home/user'?

At this point you can answer no, or press Ctrl-C to cancel the command.

-Ben
 
Old 02-02-2014, 08:00 PM   #5
kluto
LQ Newbie
 
Registered: Feb 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Ben, that sounds like the right idea.
-kluto
 
Old 02-03-2014, 01:18 PM   #6
Clovis_Sangrail
LQ Newbie
 
Registered: Mar 2012
Posts: 13

Rep: Reputation: Disabled
At first it may seem a little less convenient, but one way to avoid the dangers of leaving a stray space in the 'rm' command like you describe above is to not use cut/paste at all, instead use the filename-completion features of a shell like bash (or tcsh?). I usually only have to type the first letter or two of each directory in a long pathname, and bash writes the rest. And if a stray space (or other character) gets in there somehow the filename completion breaks right there, and tells you your mistake. And usually it's no more hassle than moving the mouse, highlighting text, etc.
 
Old 02-06-2014, 06:02 AM   #7
xamaco
Member
 
Registered: Sep 2009
Location: Bastelicaccia, Corsica
Distribution: Crux
Posts: 48

Rep: Reputation: 7
There is also the -I option. From rm manpage:

-I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes

Consider as well the --preserve-root option (which I also use in my chown and chmod aliases)
 
Old 02-06-2014, 10:06 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
1) Add an alias so when you run "rm", it automatically runs "rm -i" or "rm -I".
2) Use tab completion for finding files
3) Any time you stick a "-r" in rm, double and triple check (quintuple check if there's a "-fr") that everything is correct before pressing enter
4) Always have backups
 
Old 02-06-2014, 10:32 AM   #9
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
kluto, I will repeat suicidaleggroll's point 4 above - everyone says "keep regular backups", but there's a good reason for that. They are critical, and data loss can be devastating.

If you don't want to spend money on a USB drive, then tar up your important files, encrypt them (using AES and a strong key), and ship them off to the cloud (e.g. Dropbox, Ubuntu One, whatever). I do this on a regular basis for my laptops.
 
Old 02-07-2014, 07:44 PM   #10
Tux1000
LQ Newbie
 
Registered: Feb 2014
Posts: 10

Rep: Reputation: Disabled
@ kluto

Use the -i option or create an alias for rm with the -i option in your .basrc file in your home directory as suggested.

The only drawback to this is it will repeatedly ask your confirmation. If you are certain about the files and directories you wish to delete use the -f option with rm.

But be warned, make sure you check your syntax VERY CAREFULLY when using the -f option with rm because it overrides the interactive mode.

Last edited by Tux1000; 02-07-2014 at 07:48 PM.
 
  


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
[SOLVED] Deleting files rmcellig Linux - Newbie 13 11-26-2010 06:26 AM
LXer: Avoid latency while editing remote files using bcvi LXer Syndicated Linux News 0 07-11-2008 11:11 PM
How to avoid showing dot files sajipkuttan Linux - Newbie 3 02-27-2008 01:43 AM
About deleting a files combilli Linux - Newbie 1 01-09-2007 10:54 PM
Deleting files rossi46 Mandriva 5 11-08-2006 08:15 AM

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

All times are GMT -5. The time now is 03:55 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