View Single Post
Guest | Posts: n/a | Thanked: 0 times | Joined on
#2
Originally Posted by cybergypsy View Post
A file named 43_03_18-250208.jpg was taken on 25th February 2008 at 18:03:43.

I would like some help in writing a bash script to rename the same file to 20080225_180343.jpg so that the listing would be sorted automatically.
Sed is built in, at least on my N810:

Code:
#!/bin/sh
# camrename.sh - Renames Nokia Camera application pictures
# v0.01 2008-02-26 by Jussi Ylänen

while test -n "$1"; do
  OLDNAME="$1"
  NEWNAME="`echo $OLDNAME | sed -re 's/([0-9]{2})_([0-9]{2})_([0-9]{2})-([0-9]{2})([0-9]{2})([0-9]{2})/20\6\5\4_\3\2\1/'`"

  if test "$OLDNAME" != "$NEWNAME"; then
    # Remove "echo" below to really rename...
    echo mv "$OLDNAME" "$NEWNAME"
  fi

  shift
done
In the sed expression above, each ([0-9]{2}) matces two digits and remembers them, later to be referenced with \n (where n is a digit). Year is assumed to be 2000 or greater.

Copy-paste the script, save it in /home/user as camrename.sh and do:

Code:
~ $ chmod +x camrename.sh
Usage:

Code:
~ $ camrename.sh /media/mmc1/camera/images/*
When you've tried the script and seen that it appears to handle the names correctly, edit the script and remove the extra echo to really enable file renaming. No warranty!
 

The Following 2 Users Say Thank You to For This Useful Post: