Frequently I get messages like “I have accidentally deleted my project. Is there any way to recover it in Linux?” or “I worked really hard on that program and managed to deleted it right before submission :( How do I recover?”. Well I didn’t really have any idea on “How to actually recover deleted files in Linux”.

A few days back I was designing the new logo for Videocache in Inkscape. After finishing the design, I saved the svg file carefully. And deleted other images which I embedded in the logo. A few moments later I realized that I just screwed up myself by deleting all those files as the svg file is now good for nothing.

I searched a lot on recovering files in Linux but in vain. Then a friend (bitgeek) told me about Photorec. I managed to recover all the files using photorec. I thought it would be a good idea to let others know and spread a good word about Photorec. Below is a step by step howto on using Photorec to recover your files.

What is Photorec?

From Photorec website,

PhotoRec is file data recovery software designed to recover lost files including video, documents and archives from Hard Disks and CDRom and lost pictures (thus, its ‘Photo Recovery’ name) from digital camera memory. PhotoRec ignores the filesystem and goes after the underlying data, so it will still work even if your media’s filesystem has been severely damaged or re-formatted.

For This HowTo

Lets say I had a file download_arrow.png in /home/saini/Desktop which I have removed accidentally. Login as root and create a directory recover which will be used to store all the recovered files.

Install testdisk/PhotoRec

Photorec comes as a part of testdisk package in Fedora (I hope its same for other distributions as well). Use yum to install testdisk.

[root@fedora-tips ~]$ yum install testdisk

Launch Photorec

Once you are done with installation. Open a terminal and launch photorec (as root).

[root@fedora-tips recover]$ photorec

Select Hard Disk

If you have more than one hard disk in your system, select the one from which you have deleted the file(s).

PhotoRec Hard Disk Selection

Select Partition Type

If your hard disk has Linux partitions, then select [Intel].

PhotoRec Partition Selection

Select Filetype Option

Move to [File Opt] and press enter. Here you can disable all file types by pressing ‘s’ . Use space to toggle the check button. Now since we removed a png file, we are going to check only png file type.

PhotoRec Filetype Selection

Select Options

Photorec also has a list of different options. Under normal circumstances you don’t need to modify them

PhotoRec Option Selection

Select Partition

Move the selector to the partition from which you have removed the file. Then press enter on search.

PhotoRec Partition Selection

Select Filesystem Type

If you are using Linux, its going to be ext2/ext3/ext4. So the default selection is file.

PhotoRec Partition Selection

Select Space for Analysis

Select free if you didn’t write to that partition after removing the particular file otherwise select whole.

PhotoRec Space Selection

Select a Directory to Recover Files

Now select the path where the recovered files will be stored. Then press ‘Y’.

PhotoRec Recovery Directory Selection

Recovery Progress

Photorec will show how many files it has recovered.

PhotoRec Recovery Progress

All recovered file will be stored in the directory selected above. Open them in a file browser and you’ll get the removed file there. I hope this howto will help you recovering files you accidentally delete :)

Related Posts

{ 22 comments }

1 fdf September 1, 2009 at 3:57 AM

does it work for files deleted using terminal ‘rm -rf’ ?

2 nicu September 1, 2009 at 12:13 PM

I used Photorec a couple of times and each time I was amazed about how many files it was able to bring back. is a great tool.

@fdf: of course!

@Kulbir: you mistyped “Inkscape” :p

3 Kulbir Saini September 1, 2009 at 2:46 PM

I jumped off my chair when I saw everything I ever browsed (images) being thrown into the recovery directory :)

4 Atle Forus September 1, 2009 at 1:08 PM

Hi, and thanks for the HowTo. Great work.
I just wondered if I could edit this article and post it on my blog. But change it’s content from Fedora to Ubuntu.

Regards atlef.

5 Kulbir Saini September 1, 2009 at 2:44 PM

Thank you :) and btw did you hear me saying “Why Not??” :D

6 nicu September 1, 2009 at 3:03 PM

You may want to consider adding a CreativeCommons license to those articles, so the people feel invited to reuse the content (with proper credit).

7 Kulbir Saini September 1, 2009 at 4:18 PM

nicu,

Thanks for the tip. Added it to footer :)

8 Atle Forus September 1, 2009 at 8:01 PM

Well, I searched your site for Creative commons/GPL *.*.
And when I did not find anything related to your articles, I needed to ask for your permission.
Thank you for the quick answer. And I will send the article to you before I publish it at my blog.

Regards,
atlef.

9 Kulbir Saini September 1, 2009 at 9:13 PM

I am sorry for that. Added it in footer now :)

10 Atle Forus September 1, 2009 at 9:37 PM

That is quiet all right. We were more or less on the same page, as I thought that these articles were “yours”, hence the question. And you probably did not “think” that it was necessary to include this information.
So this is a win, win situation. I, and those reading your articles, know this. And you contribute to the community to let us edit these to provide information to all and everyone.

atlef.

11 pranesh September 1, 2009 at 4:05 PM

HI dude awesome post..
Thanks a lot…

12 Jan Mars September 3, 2009 at 2:15 PM

I installed Photorec but it seems unable to find the camera that the lost files contains.
Where puts Ubuntu a usb-camera?
Thanks.
J

13 nicu September 4, 2009 at 2:38 PM

Does your camera work as USB mass storage or as a PTP device?

When I recovered files from my camera, I just got the memory card out and put it on a card reader, so it is surely accessed as mass storage and mounted properly (my camera is a Canon SLR, which is normally accessed with PTP).

14 Jim Wilkins October 17, 2009 at 6:26 AM

After a trip to Greece, I had been trying to burn videos to DVD via dedicated burner (videocam and dedicated burner by Sony) The camera’s internal memory (full of previous recordings) had been archived once on DVD. When I tried to archive the memory stick which had the Greece trip, it said ‘all data had been previously backed up’ Whatever. I finally followed the instructions~verbatim to delete all from internal memory and, wouldn’t you know it, it deleted all of the memory stick instead! I thought I was sunk. PhotoRec did it’s majick and faithfully reproduced most, if not all, of the clips. And considering it is GPL and free, as in beer, I could not be happier. Thanks to all!!!

15 Nandakumar December 14, 2009 at 7:26 PM

We have tried this in our server to restore deleted database folder. But unfortunately, we didnt got back the folder structure. What we got was list of files with some ordered name like file1, file2 … so on.. We cant identify the files, which it belongs to. Can you help us how to restore the files ?

16 steve March 25, 2010 at 11:02 PM

i try with photorec to recover files from a formated disk and i try to use an external disk (connected by usb port) to save the files.i saw all the steps that are in the guide and the name of the disk is sdc but i can’t selected it in the final step where photorec need to know where the recovered files can be saved.does anyone have any idea about that or how i can run photorec with commands without steps??if someone know the solution please send me mail.thaks!

17 Sandcaster February 24, 2011 at 1:06 PM

I am having a similar problem and was wondering if you found an answer to how to select an external drive.

18 Jan Mars March 1, 2011 at 9:06 AM

It is a while ago, but i seem to remember that it was something with the camera. After i put the memory card in another camera, photorec worked fine. See the comment of Nicu (sept 4, 2009) above.

19 Hans July 7, 2010 at 3:37 PM

Thanks for the howto it helped me a lot.

Rgds

Hans

20 James November 5, 2010 at 3:47 PM

Can’t you specify a certain folder to search rather than the whole drive? I only need to search my images folder

Cheers

21 PaT November 8, 2011 at 9:42 AM

I know it’s a bit old post, but thanks a lot. I was amazed how many files it was able to recover, almost everything i was working on from couple of months. And of course helped me to recover my .rtf file i accidentally deleted. Great work, and thanks a lot for your help.

22 Rick July 23, 2012 at 12:04 AM

Will this work with ptp? I have a Galaxy Nexus (non removable sd card). Win 7 will not recognize my devise as a drive. Most forensic programs ive found need ums to work. Does anyone have a work around? I unlocked and rooted and lost all pics

Comments on this entry are closed.

Previous post:

Next post: