Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I accidentally deleted a file from my laptop. I'm using Fedora. Is it possible to recover the file?

share|improve this question
5  
whats your filesystem? –  echox Oct 3 '10 at 19:56

5 Answers 5

up vote 35 down vote accepted

I would advise against immediately installing some utility. Basically your biggest enemy here are disk writes. You want to avoid them at all costs right now.

Your best bet is an auto-backup created by your editor--if it exists. If not, I would try the following trick using grep if you remember some unique string in your .tex file:

$sudo grep -i -a -B100 -A100 'string' /dev/sda1 > file.txt

Replace /dev/sda1 with the device that the file was on and replace 'string' with the unique string in your file. This could take some time. But basically, what this does is it searches for the string on the device and then returns 100 lines before and after that line and puts it in file.txt. If you need more lines returned just adjust the -B and -A options as appropriate. You might get a bunch of extra garbage returned, but you should be able to get your text back.

Good luck.

share|improve this answer
3  
Somebody showed me this a couple weeks after I switched to Linux, when I killed a text document I really needed -- it pretty much blew my mind –  Michael Mrozek Oct 3 '10 at 21:03
5  
By the way, -B100 -A100 can be replaced by equivalent -C100 (C for "context"). And I would definitely not use -i if it were possible, but have -F in its place. –  rozcietrzewiacz Aug 13 '11 at 23:16
    
Probably unsurprisingly -- I assume the same disk blocks have been reused -- this method did not work for a file which I accidentally overwrote, not accidentally deleted. –  ntc2 Dec 29 '13 at 7:41
    
That is really a great trick! –  Matthias Apr 5 at 14:22

It is possible, its just going to be a hassle.

UPDATE: before you try this method, please have a look at Steven's answer

You're going to need the testdisk package, a lot of disk space and a lot of time.

PhotoRec, a part of TestDisk, can recover files from almost any disc. PhotoRec does support finding .tex files


First, install testdisk by running

yum install testdisk

note: Your going to need a lot of free space on another drive, where you can save recovered files.

Recover all the deleted files on your disc by running photorec on the free space of the disc.

sudo photorec

and follow the instructions... ( remember not to save the files to the same disc you are recovering from )

After the proccess has completed, all the recovered files should be in one directory, where you should run:

find -name '*.tex' > filelist

This will output a list of files that might be the one you lost. You will have to check all of them, as the filenames will be lost.

share|improve this answer
6  
photorec is a good util, I would just be worried about the disk writes that are going to happen during install. Is there anyway to get this as a standalone executable that can run from external media? –  Steven D Oct 3 '10 at 20:13
1  
Yes, you are very right. I don't know about a standalone executable, but one could try using some live-cd? –  Stefan Oct 3 '10 at 20:21
2  
to cut down on the files recovered with Photorec, the third screen in will give a [File Opt] select this. You can from a list select what file types you want to or do not want to be recovered. –  Steve Burdine Oct 3 '10 at 20:39

Many text editors keep backup files. If you are really lucky, there might be something like yourfile.tex~ including a previous version of your file.

share|improve this answer
    
+1, true... a much more simple and elegant solution :) –  Stefan Oct 3 '10 at 20:07

There are other data recovery tools and the most effective are foremost, photorec, scalpel and magic rescue. (I'm assuming that the 'grep' trick told here is not enough) Here you can find some tutorials about how to use them:

http://www.howtogeek.com/howto/15761/recover-data-like-a-forensics-expert-using-an-ubuntu-live-cd/

https://help.ubuntu.com/community/DataRecovery

and finally:

http://technology-flow.com/articles/backtrack-5-complete-tut/forensics/

share|improve this answer

NOTE: I added this answer regarding some other question about deleted database files (MySQL server) which was closed and pointed to this one. I believe it can be useful in some other similar situations too (as far as some process still holds the file descriptors open).

If your process is still runnig then you can find your files in /proc/<pid>fd/ and just copy them. Send SIGSTOP first to the process group. Copy the files. Build new instance on side and keep this one stopped or kill -9 it and put the files back on their places. InnoDB will recover by its own when run but if there was some MyISAM than you'll have to do this manually.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.