{"id":182,"date":"2011-05-07T13:57:51","date_gmt":"2011-05-07T19:57:51","guid":{"rendered":"https:\/\/www.joshualyman.com\/?p=182"},"modified":"2013-03-23T05:46:58","modified_gmt":"2013-03-23T11:46:58","slug":"using-photorec-and-a-bash-script-to-recover-51407-images","status":"publish","type":"post","link":"https:\/\/www.joshualyman.com\/2011\/05\/using-photorec-and-a-bash-script-to-recover-51407-images\/","title":{"rendered":"Using Photorec and a bash script to recover 51,407 images"},"content":{"rendered":"

A sad thing happened to my mother-in-law the other day. Somehow, one of her 1 TB hard drives that stores mountains of images (she is a professional photographer) got reformatted to a nice, fresh state, but certainly not what was wanted. She has a client that needs the images in just a couple of days, and so the race was on to see what I could do (remotely, using LogMeIn no less) to recover the data.<\/p>\n

I turned to Photorec<\/a>, and absolutely fantastic command line program that does a hard core scavenge of any disk, looking for known file headers, and tries to reconstruct a file based on its findings. I felt that, since the drive had simply been reformatted and not zeroed, with only one file being placed on the drive since the formatting, that the chances of finding the files was pretty good. Using another external hard drive as storage for recovered files, 15 hours later it had recovered 51,407 .JPGs, .NEFs, and .PSDs, woohoo!<\/p>\n

Now one unfortunate byproduct of Photorec recovery is that, while much of the file’s metadata is still in place (modified\/accessed timestamps, EXIF data, etc.), it didn’t recover filenames or folder structure. So I had over 51,000 images with random filenames in 130 different “recup” folders, mostly in chunks of the same creation date, but not entirely.<\/p>\n

To solve this, I wrote a small little bash script to sort files into folders by modified date. I’m not super skilled at bash scripting, so it took me longer than it probably should of and is probably not very elegant, but it worked quite well in my case! I pointed the script at the recovered directories, ran it, and 3 minutes later it had created over 1300 folders based on the created\/modified dates of the files, moved the files into the folders, and bam! I was pretty impressed with myself. \ud83d\ude42<\/p>\n

If anyone is looking for a similar script (couldn’t find one after an initial Google search last night) then feel free to take this one and modify to suit your needs. Obviously I can’t guarantee anything with it, but it shouldn’t eat your files if you set up the directories correctly. If you are nervous, change the “mv” command on line 27 to “cp” and you should be safer (although the script will of necessity run much slower). There are certainly some things that could be improved about the script (not hardcoding the 300 of ## number, not cd’ing into the source directory, etc.) but it worked in a pinch and didn’t need to be elegant. Hope it helps!<\/p>\n

#!\/bin\/bash\r\n# Sort files into folders based on their creation date.\r\n#\r\n\r\n# Indicate a path to the source folder of data to be sorted, and a destination folder where you want the sorted folders to be.\r\n# Note: this will work better if the sorted directory is NOT inside the source directory, otherwise recursion might occur and the world might blow up.\r\nSOURCE_DIR=~\/Desktop\/testsrc\/\r\nSORTED_DIR=~\/Desktop\/sorted\/\r\nsorted_count=0\r\n\r\n# go into the first folder of the source folder\r\ncd \"$SOURCE_DIR\";\r\n\r\n# for each item in this folder\r\nfind . -type f -print | while read file; do \r\n\r\n\t# Get the creation date of the item and store it in a variable\r\n\tfile_date=`stat -f '%Sm' -t \"%F\" \"$file\"`;\r\n\r\n\t# Does a folder with this date already exist?\r\n\tif [ ! -d \"$SORTED_DIR\/$file_date\" ]; then\r\n\t\t# If no, create the folder\r\n\t\tmkdir -p $SORTED_DIR\/$file_date\r\n\tfi\r\n\r\n\t# Now move the file to the folder\r\n\tmv \"$file\" \"$SORTED_DIR\/$file_date\/\"\r\n\r\n        # Provide some feedback to the user on our progress\r\n\tlet \"sorted_count += 1\"\r\n\tif [[ \"$sorted_count % 100\" -eq \"0\" ]]; then\r\n\t\techo \"Sorted $sorted_count of 51,407\" # Obviously the of count is hard coded \r\n\tfi\r\n\r\ndone\r\n\r\necho \"Finished.\"<\/pre>\n","protected":false},"excerpt":{"rendered":"

How do you recover thousands of images from a 1 TB formatted hard drive and make sense out of them? Use Photorec to recover the images, and then write a small bash script to sort those gibberish files into folders created based on date modified! Sample script included.<\/p>\n