Skip to content


Recover deleted files in ext*

rm is a lovely command, isn’t it? Especially when you make a typo in a shell script. Let’s see how to recover deleted files.

sudo apt install testdisk
mkdir mnt/different-device/recovery-folder

Start the recovery program:

sudo photorec

Unselect all file types and only check those that you want to search for (e.g. text files), then confirm with b.

Select the directory just created and start the recovery process. Once done (or even during the process, even though you’ll probably miss the most recent versions of the file you want to recover):

cd /mnt/different-device/recovery-folder

sudo find . -path "./recup_dir.*" -type d -exec chown -R user:user {} \;

find . -path "./recup_dir.*" \( -type f -name "*.svg" -o -name "*.ly" -o -name "*.java" -o -name "*.csv" -o -name "*.pm" -o -name "*.py" ...etc. \) -delete

Keep in mind though that the extensions are just put there by photorec and may not reflect the original extension of the file. For example, a *.md file might get the *.html extension because it contains some HTML. Therefore, do not delete files with extensions that could even remotely make sense for what you’re searching for; or simply do not delete files at all, if you have enough space on the device.

Finally:

clear ; grep -nr 'some_smart_keyword' recup_dir.* | sort -t '.' -n -k2,2

The last file should be the most recent.