Bash Script to Sort Photos in Directories based on Exif date information in Qnap

Last week, I showed you how to write a small script to automate photo uploads from your android phone to an ftp server ( From SGSIII to Qnap in my case), by running a bash Script in Smanager.

today, I will show you how to install exif program on your Qnap box and use it to sort the photos in directories created based on the Exif Date. If you are like me, you surely have several photos every week, that add up to several hundreds every month and navigation become difficult. I personally like my photos sorted into folders based on the date they were taken.

This script will read exif info from your file, then create a directory based on exif creation date and finally move your file into that directory.

1. Install Exif tool using Optware frontend.

Exif is a very light weight utility to retreive exif information from your photos. You can install it by going to your optware ipkg webfront ( typically on http://yourmachineip/Optware). If optware is not installed you can install it using QPKG manager.

Search for Exif, and click on Install task. It would display a few lines of information as it downloads, extracts and intalls the program.

2. Write a Script to retretive the exif information from your jpeg files and create directories based on them.

Here is the script that I use to move my picture files in directories based on their exif creation date. SSH into your Qnap box, create this script, CHMOD to 755 and run it in the directory you have pictures.

for fil in *.jpg
datepath="$(exif $fil | grep origi |awk '{print $4}'| sed s/'(origi|'/''/ | sed s/':'/'.'/g)"
echo $fil has datestamp $datepath
if ! test -e "$datepath"; then
echo creating directory $datepath
mkdir "$datepath"
echo moving $fil to $datepath
mv $fil $datepath

This script will first check the exif data, then create directories based on exif data and finally move the files to respective directories. The 2 Echo statements are optional, but I like to see what my script is doing so I have put them there.

Why I use Exif is becusae it’s very fast, capable of processing several hundred files in seconds. within seconds of running your script, you will see all the directories created and files falling in their respective directories.