In order to keep multiple versions of a file, I sometimes add a timestamp to the filename. Doing this by hand is tedious and error-prone, but this chore can be automated by a small shell script.
/usr/local/bin/timestamp
#! /bin/sh for oldname ; do ts=`date +'%Y%m%d%H%M' --reference="$oldname"` newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(-[[:digit:]]{12})(\\..*)\$/\\1\\2-$ts\\4/p"` if [ -z "$newname" ]; then newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(\\..*)\$/\\1\\2-$ts\\3/p"` fi if [ -z "$newname" ]; then newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(-[[:digit:]]{12})\$/\\1\\2-$ts/p"` fi if [ -z "$newname" ]; then newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)\$/\\1\\2-$ts/p"` fi if [ -f "$newname" ]; then if [ "$newname" != "$oldname" ]; then echo "Can't rename $oldname to $newname: file already exists." fi else echo mv "$oldname" "$newname" mv "$oldname" "$newname" fi done
As I’m using KDE as my desktop, I like to have the timestamping integrated. Fortunately, it’s easy.
~/.kde/share/apps/konqueror/servicemenus
Actions=timestamp ServiceTypes=all/all [Desktop Action timestamp] Exec=/usr/local/bin/timestamp %u Icon=clock Name=Timestamp
See Creating Konqueror Service Menus for a general introduction.
Update, 2007-02-06
I’ve updated the script to insert the timestamp before all the first extension and to also work with filename starting with a ‘.
‘.