How to recover deleted files on Linux

On a typical file system, deleting a file doesn't necessarily mean that it is gone for good. When a file is removed, its meta data (e.g., file name, size, time, location of data block, etc.) is gone, but actual file data is untouched inside the file system, until the location of the data is overwritten by other file data. This means that if you accidentally deleted a file, there is a chance that you can recover the file.

In this tutorial, I describe how to recover deleted files on Linux. There are several file recovery tools on Linux. Among them is PhotoRec which is an open source file recovery software licensed with GPLV v2+. PhotoRec is available on Linux, BSD, MacOS X and Windows.

PhotoRec Features

As the name implies, PhotoRec is originally designed to restore accidentally deleted digital photos. However, now it has become versatile enough to support various file formats. PhotoRec recovers lost files by checking data blocks one by one against a signature database of different file types.

  • Supported file formats: video (avi, mov, mp3, mp4, mpg), image (jpg, gif, png), audio (mp3, ogg), document (doc(x), ppt(x), xls(x), html), archive (gz, zip) etc.
  • Supported file systems: EXT2, EXT3, EXT4, HFS+, FAT, NTFS, exFAT

Besides hard disks, PhotoRec can restore files stored on CD/DVD drives, USB sticks, memory cards (CompactFlash, Memory Stick, Secure Digital/SD, SmartMedia), etc. So if you accidentally lost digital pictures stored on the memory card of a digital camera, you can use PhotoRec to undelete them.

Install PhotoRec on Linux

The official site offers PhotoRec binaries for various platforms. So you can download static PhotoRec binary for your Linux system.

For 32-bit Linux:

$ wget http://www.cgsecurity.org/testdisk-6.14.linux26.tar.bz2
$ tar xvfvj testdisk-6.14.linux26.tar.bz2

For 64-bit Linux:

$ wget http://www.cgsecurity.org/testdisk-6.14.linux26-x86_64.tar.bz2
$ tar xvfvj testdisk-6.14.linux26-x86_64.tar.bz2

The PhotoRec executable (photorec_static) is found in the extracted directory.

Recover Deleted Photos and Videos

In this tutorial, I demonstrate how to recover deleted photos and video files stored on an SD card, which were generated by Canon EOS Rebel T3i.

When you have removed a file accidentally, what's important is to NOT save any more files on the same disk drive or memory card, so that you do not overwrite the deleted file.

As soon as you discover the lost files, run PhotoRec to restore them as follows.

$ sudo photorec_static

You will be shown a list of available media. Choose the media where you have deleted files.

Next, choose the partition which contains deleted files.

Choose the file system type used for the partition. In general, you can identify the file system type from the output of mount command. In case of the SD card used by Canon camera, it is formatted in VFAT file system. So choose "Other".

Choose if all disk space needs to be analyzed. In this case, choose "Free", which means scanning for unallocated space only.

Choose a destination folder where restored files will be stored. Here you must choose a different partition or drive than the one being analyzed. Press "C" when a destination is chosen.

Now PhotoRec starts reading individual sectors for lost files. You will see the progress of the recovery. Depending on the size of media, it will take a couple of minutes or even longer.

After scanning is completed, the restored files will be stored in the destination folder that you configured. Note that the size of a restored file may be either the same as or larger than the original file size.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

5 thoughts on “How to recover deleted files on Linux

  1. PhotoRec works with VFAT too, so you can recover photos from the memory card used for the camera.

  2. I used PhotoRec on my wife's 8GB SD card - 2 hours later we'd recovered every photo she'd ever taken onto that card.. bar the one photo she really needed. Otherwise it's a genuinely great tool.

  3. PhotoRec is simply fantastic. The documentation needs improvement, as I did not know how to invoke it after the install. Extremely simple to invoke: just type "photorec" in a terminal, and voila. The script guides you through the process with basic menus. It recovered files from the SDCard that appeared to be lost when the o/s froze up on me during the copy from card to external drive. Photorec recovered the files in minutes. Compared to anything used prior in Windows, which required hours to complete, this is light years ahead. I am very grateful to the developers for this extremely useful application.

  4. When I selected the external USB drive, Photorec displayed the contents of the internal drive instead. Extundelete worked and restored the contents to my home folder on the boot drive. Would be nice to see a GUI for it, but this did the job:

    umount /dev/sdc1
    sudo extundelete --restore-all /dev/sdc1

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *