Lumalab

Search entriesDeutsch English
  
User:   Password:    
Forum > Linux > Rename a numbered file sequence
StartStopPage 1 of 1StopEnde
Author: EngysTopic: Rename a numbered file sequenceLast change: 2014-11-06 14:04:09 

I think this script speaks for itself:

#!/bin/bash

if [ "$1" == "" ]; then

echo
echo "serename 1.0 is a lumalab.net bash script to rename a numbered file sequence."
echo "This script using sed and zenity for additional UI integration."
echo "Usage as xfce custom action command: serename.sh %n"
echo
echo "Shell usage: serename.sh <oldname>.<ext> <newname>"
echo
echo "Example: serename.sh oldname0122.png newname"
echo
echo "         This example will rename a numbered file sequence"
echo "         in current directory with the name oldname and the extension png"
echo "         into newname with existing digits and extension."
echo
echo "This script has the intension to be useful but comes without any warranty."
echo "Feel free to modify it for your own purpose."
echo

else

IN_NOEXT=`echo "$1" | cut -d'.' -f 1` # remove extension
IN_NAME=`echo "$IN_NOEXT" | sed 's/[0-9]*//g'` #remove all digits

if [ "$2" == "" ]; then
NEW_NAME=$(zenity --entry --text="New name:")
else
NEW_NAME=$2
fi

IN_EXT=`echo "$1" | cut -d'.' -f 2-` # get selected extension

# force that input name has an extension
if [ "$IN_EXT" == "$IN_NOEXT" ]; then
IN_EXT=$(zenity --entry --text="Extension:")
fi

OUT_NOEXT=`echo "$2" | cut -d'.' -f 1` # remove extension
OUT_NAME=`echo "$OUT_NOEXT" | sed 's/[0-9]*//g'` #remove all digits

for FILENAME in $IN_NAME*.$IN_EXT; do

   OUT_EXT=`echo "$FILENAME" | cut -d'.' -f 2-` # get extension
   OUT_DIGIT=`echo "$FILENAME" | sed 's/[^0-9]*//g'` # get digits

   mv "$FILENAME" $NEW_NAME$OUT_DIGIT.$OUT_EXT  

done

fi

UpReply 
StartStopPage 1 of 1StopEnde