Thursday, June 24, 2010

Batch rename files using their modification date

Unlike JPG images, video files do not include some kind of EXIF table.

An easy way to remember the date your video was shot, is to include the shot date in the filename.

In my case I had tons of small video files and the only clue about the shot date was their modification date. What I needed, was a small script to rename all the video files and include each file's modification date in the new filename.

This was a bit tricky because I had to handle spaces in directory names. The key was to temporarily modify the internal field separator (IFS) to exclude space. Here is the script.

#!/bin/bash
OLDIFS=$IFS

IFS=$'\n'
for file in `find . -name '*.mp4'`
do

dir=`dirname "$file"`
filen=`basename "$file"`

moddate=`stat -c %y "$file"`
fdate=`date -d "$moddate" "+%Y-%m-%d"`

echo Renaming file: $filen ..
mv $file $dir/$fdate-$filen
done

IFS=$OLSIFS

No comments:

Post a Comment