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.
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:
$ tar xvfvj testdisk-6.14.linux26.tar.bz2
For 64-bit Linux:
$ 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.
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
You can have daily Linux tutorials and FAQs delivered to your email inbox. Simply enter your email address below to subscribe to our mailing list. You will receive hands-on guides and carefully written tutorials related to Linux, everything for free.