LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-16-2009, 11:46 PM   #1
baldurpet
Member
 
Registered: Dec 2008
Location: Iceland
Distribution: Ubuntu, freeBSD
Posts: 110

Rep: Reputation: 15
What is the best action to take if you delete a file and want to recover it?


No, I haven't done anything stupid- but I have a hunch that I'm going to use rm (or god forbid something like rm -fr) recklessly some day and delete something important.

And because luck favors the prepared, I wanted to know what would be the best course of action to take in such an event? I'm guessing you'd first of all want to shut down you computer right away (because some programs or daemons could overwrite the place where you data was right?) and then you'd download some file recovery software for Linux?

This is just a guess though and I have no idea how it would e.g. vary between file systems.

Last edited by baldurpet; 02-16-2009 at 11:47 PM.
 
Old 02-17-2009, 07:19 AM   #2
DragonSlayer48DX
Registered User
 
Registered: Dec 2006
Posts: 1,454
Blog Entries: 1

Rep: Reputation: 75
Quote:
Originally Posted by baldurpet View Post
I'm guessing you'd first of all want to shut down you computer right away (because some programs or daemons could overwrite the place where you data was right?) and then you'd download some file recovery software for Linux?
You're partly right- the data area can be quickly overwritten, but downloading a recovery tool after the fact would also do the same...

Here are a couple of tutorials on the subject: #1 and #2.

Cheers

Last edited by DragonSlayer48DX; 02-17-2009 at 07:21 AM.
 
Old 02-17-2009, 07:56 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The best action to take is not to take that action ... or to prevent it. What I did is I make a wrapper script around rm, called it srm (safe rm) and I use that as root. The script prevents you from deleting top-level directories.

Code:
#!/bin/sh

# this script takes only 2 arugments total
if test "$#" != 2
then
  # fail
  echo 'ERROR: This script requires exactly 2 arguments'
  exit 1
fi

# don't delete things 2 levels from the root directory
if find / -type d -maxdepth 2 | grep "$2" 1> /dev/null
then
  # fail
  echo "ERROR: Bad idea, will not remove $2"
  exit 1
fi

rm "$1" "$2"

# success
exit 0
If you want, another strategy is to make a blacklist of files that you don't want deleted, probably you can generate it using 'find'.
 
Old 02-17-2009, 08:27 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I always found backups handy ...
And bad experience is generally a very good teacher.
 
Old 02-17-2009, 02:54 PM   #5
baldurpet
Member
 
Registered: Dec 2008
Location: Iceland
Distribution: Ubuntu, freeBSD
Posts: 110

Original Poster
Rep: Reputation: 15
I was talking about what you should do in the event of deleting something important.

That is to say "I removed my home directory 2 seconds ago, what should I do RIGHT NOW?", because I want to know what I should do
 
Old 02-17-2009, 03:17 PM   #6
DragonSlayer48DX
Registered User
 
Registered: Dec 2006
Posts: 1,454
Blog Entries: 1

Rep: Reputation: 75
Quote:
Originally Posted by baldurpet View Post
I was talking about what you should do in the event of deleting something important.

That is to say "I removed my home directory 2 seconds ago, what should I do RIGHT NOW?", because I want to know what I should do
The tutorials for which I provided links explain in great detail.

The others gave insight on how to be prepared.
 
Old 02-17-2009, 04:11 PM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by baldurpet View Post
That is to say "I removed my home directory 2 seconds ago, what should I do RIGHT NOW?", because I want to know what I should do
And I'm saying that you should prevent this from happening, because that's the only reliable way. You can go ahead and try all the methods above as well as:
http://tldp.org/HOWTO/Ext2fs-Undeletion-4.html
http://tldp.org/LDP/LGNET/156/misc/l..._recovery.html
also data carvers like foremost and testdisk will come in handy. BUT, it's very likely that these methods will almost never lead to 100% recovery. So, the best way is to prevent it from happening.
 
Old 02-17-2009, 07:58 PM   #8
vibinlakshman
Member
 
Registered: Dec 2008
Location: Kerala, India
Distribution: Ubuntu 11.10
Posts: 334

Rep: Reputation: 33
Wink

Quote:
Originally Posted by H_TeXMeX_H View Post
The best action to take is not to take that action ... or to prevent it. What I did is I make a wrapper script around rm, called it srm (safe rm) and I use that as root. The script prevents you from deleting top-level directories.

Code:
#!/bin/sh

# this script takes only 2 arugments total
if test "$#" != 2
then
  # fail
  echo 'ERROR: This script requires exactly 2 arguments'
  exit 1
fi

# don't delete things 2 levels from the root directory
if find / -type d -maxdepth 2 | grep "$2" 1> /dev/null
then
  # fail
  echo "ERROR: Bad idea, will not remove $2"
  exit 1
fi

rm "$1" "$2"

# success
exit 0
If you want, another strategy is to make a blacklist of files that you don't want deleted, probably you can generate it using 'find'.

I was searching for this kind of thread , script seems to be ok for me , but i need to knw hw u did u make wrapper , i dont knw to make a wrapper , can u cite frm above script how did u made it .. Any way thanks for the script , if possible can u please explain hw this script works ...!!!!!
 
Old 02-18-2009, 03:17 AM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You copy all that into a text file called 'srm'. Then you make the text file executable. Then you move or symlink the text file into your path (type 'echo $PATH' to see where).

How the script works is rather simple:
Code:
if find / -type d -maxdepth 2 | grep "$2" 1> /dev/null
then
  # fail
  echo "ERROR: Bad idea, will not remove $2"
  exit 1
fi
This is the main piece of code. It uses find to list all directories 2 levels deep from / and if grep finds a match among those the script will refuse to delete.

Last edited by H_TeXMeX_H; 02-18-2009 at 03:24 AM.
 
Old 02-18-2009, 08:54 AM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
One day, I managed to delete the contents of /bin (or was it /usr/bin? I forget...).

Sure was a good thing that I use rsync for a daily backup regimen. I went into the backup directory, and copied the entire contents of the missing directory back into place.
 
Old 02-18-2009, 02:58 PM   #11
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
i would have to agree that while possible, data recovery is dicey at best, and should be a last resort
having a backup to fall back on would be the best option regardless of the method of backing up

i lost count of how many times i ended up reinstalling linux on my machine when i was learning it became part of my routine for a while, now the only time i reinstall linux is to try a different distro or have a fresh machine to install

Last edited by frieza; 02-18-2009 at 03:00 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
If I know the sector position and file size, is this sufficient to recover a file? jsstevenson Linux - General 2 09-29-2008 10:35 AM
delete dev direcory , how can recover? jimmyjiang Red Hat 5 10-18-2006 02:38 PM
file monitoring + take action tek1024 Linux - Software 3 11-18-2004 06:41 PM
Changing the default open action for an executable file nitrambass LinuxQuestions.org Member Intro 1 06-22-2004 03:09 PM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 02:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:44 AM.

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