User Tools

Site Tools


howtos:move_pictures_by_date

Reorganize script

This script takes one parameter, the directory to where your pictures (or files) resides. It then creates a directory with the name of the creation date and move the picture/file into it. The directory name is in this format: YYYY-MM-DD

#!/bin/bash

for i in $(ls $1); do
	CREATEDATE=$1/`date -r $1/$i +%F`
	SRC=$1/$i
	if [ -d $CREATEDATE ]; then
		mv $SRC $CREATEDATE
	else
		mkdir $CREATEDATE
		mv $SRC $CREATEDATE
	fi
done

This can be used to organize pictures into a folder structure easily used by a gallery (that's at least what I'm using it for).

Hope you like it.

Update: If you want to use underscore as separators in the directory names use this edition instead:

#!/bin/bash

for i in $(ls $1); do
	CREATEDATE=$1/`date -r $1/$i +%Y_%m_%d`
	SRC=$1/$i
	if [ -d $CREATEDATE ]; then
		mv $SRC $CREATEDATE
	else
		mkdir $CREATEDATE
		mv $SRC $CREATEDATE
	fi
done

By changing the date command (see man pages) you can make a naming layout that suite you best. Just change the parameters after the “+” sign.

Update #2:

If you have a bunch of jpg files and you've lost their creation date you can extract it from an exif tag (if it is set by the camera, wich they usually are). This script will do just that. Note however that it is ONLY jpg files:

Remember to install exiftool (sudo apt-get install libimage-exiftool-perl)

#!/bin/bash

for i in $(ls $1); do
	CREATEDATE=$1/`exiftool -d %Y_%m_%d -CreateDate $1/$i |cut -d: -f2|  sed -e 's/^[ \t]*//'`
	SRC=$1/$i
	if [ -d $CREATEDATE ]; then
		mv $SRC $CREATEDATE
	else
		mkdir $CREATEDATE
		mv $SRC $CREATEDATE
	fi
done
howtos/move_pictures_by_date.txt · Last modified: 02/12/2018 20:34 by 127.0.0.1