97

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

1
  • 11
    whats your filesystem? Commented Oct 3, 2010 at 19:56

8 Answers 8

127

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.

14
  • 9
    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 Commented Oct 3, 2010 at 21:03
  • 26
    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. Commented Aug 13, 2011 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. Commented Dec 29, 2013 at 7:41
  • 1
    Thanks it worked! Amazing how much of one's old stuff is floating around the hard drive. It is as if you went out into the ocean and with a gigantic net pulled in all kind of floating debris which you then have to sort through.. Commented Nov 17, 2020 at 18:41
  • 1
    +1. If the system has another drive available (e.g. a SSD or HDD, a ramdisk or a USB stick) then I recommend writing the output to that rather than to the same drive you're trying to recover from. As this answer says "Basically your biggest enemy here are disk writes". If you don't have another drive available, you can quickly create a tmpfs on /mnt with mount -t tmpfs none /mnt (default is to make a ramdisk with size equal to half of your RAM. you can change that with -o size=nnnn option) Commented Nov 29, 2021 at 7:05
27

It is possible, it's 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: You're 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 process 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.

3
  • 13
    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? Commented Oct 3, 2010 at 20:13
  • 2
    Yes, you are very right. I don't know about a standalone executable, but one could try using some live-cd? Commented Oct 3, 2010 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. Commented Oct 3, 2010 at 20:39
14

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.

0
10

There are other data recovery tools; the most effective are foremost, PhotoRec, scalpel and magic rescue.  (I'm assuming that grepping against the disk, as discussed in Steven D’s answer, is not enough.)  Here you can find some tutorials about how to use them:

4

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.

4

In any case I'd stop working on the device being used as soon as possible to avoid any disk writes, and boot into a dedicated recovery OS, like SystemRescueCd, which is a Live-CD so you can mount your disk read-only in order to prevent further data loss.

Those distros include a lot of recovery tools mentioned by others, and you can install most missing.

I always tend to have a CD with a recent version of SystemRescueCd in my external USB reader, just in case.

2

I had accidentally deleted my sqlite db file.

What I did to find out the file was,

Opened the location /proc/ in a file browser and searched for the deleted sqlite db file over there.

I found out that file in a search result. Copied that file from there to my old place.

1

I had the same problem two years ago and I tried a lot of programs, like debugfs, photorec, ext3grep and extundelete. ext3grep was the best program to recover files. The syntax is very easy:

ext3grep image.img --restore-all 

or:

ext3grep /dev/sda3 --restore-all --after date -d '2015-01-01 00:00:00' '+%s' --before `date -d ‘2015-01-02 00:00:00’ ‘+%s’ 

This video is a mini tutorial that can help you.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.