Renaming Files based on mtime

2 minute read

So I have this cool little camera, the JVC GZ-MS100R, a SD camera that generates simple MPEG-2 files. The filenames are however a bit uncool:


This doesn’t really help when you are looking for the movies you made on christmas eve.

So I checked the files a bit closer and noticed that the mtime information is the time that the clip was recorded. So I wrote two simple shell scripts. One that loops through all entries in my directory that contains the clips and calls the second script that renames the file to the mtime content and changes the extension to .mpg. So the new filenames look like this:


So here the first simple script,


for i in $(ls *.MOD); do
./ $i

Wow 🙂 As you can see, it simply looks for files ending with .MOD and calls the second script,


echo “Working on $1”

YEAR=`ls -l –full-time $1 | awk ‘{print $6 }’ | cut -d ‘-‘ -f 1`
MONTH=`ls -l –full-time $1 | awk ‘{print $6 }’ | cut -d ‘-‘ -f 2`
DAY=`ls -l –full-time $1 | awk ‘{print $6 }’ | cut -d ‘-‘ -f 3`

HOUR=`ls -l –full-time $1 | awk ‘{print $7 }’ | cut -d “:” -f 1`
MINUTE=`ls -l –full-time $1 | awk ‘{print $7 }’ | cut -d “:” -f 2`
SECOND=`ls -l –full-time $1 | awk ‘{print $7 }’ | cut -d “:” -f 3 | cut -d “.” -f 1`

mv $1 renamed/$NEWNAME.mpg

This one extracts the mtime information from the file to construct the new file name. It moves the file from its current place to the directory renamed from where I copy it to my media server.

Some more details. The –full-time option to ls gives me the ISO time representation I need. The date part looks like this: 2008-12-24. Now with cut and “-” as the delimiter I can simply collect the YEAR, MONTH and DAY. The time is similar, however it uses “:” as the delimiter and I need to strip the seconds down to just that.

Done 🙂

Next step: The camera gernerates MPEG-2 with AC3 audio. My mediaplayer (MythTV) doesn’t like that. So lets use ffmpeg to convert the audio to plain simple mp2 stereo. In my next post 🙂