Lumalab

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

I think this script speaks for itself:

#!/bin/bash

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

echo
echo "renum 1.1 is a lumalab.net bash script to renumber a file sequence."
echo "This script is using sed and zenity for additional UI integration."
echo "Usage as xfce custom action command: renum.sh %n"
echo
echo "Shell usage: renum.sh <name>.<ext> <new_starting_number>"
echo
echo "Example: renum.sh image0122.png 200"
echo
echo "         This example will renumber a file sequence"
echo "         in the current directory with the name image"
echo "         and the extension png based on the new starting number 200"
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

if [ "$2" == "" ]; then
NEW_START=$(zenity --entry --text="New starting number:")
else
NEW_START=$2
fi

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

# Create temp files to prevent overwriting
COUNTER=$NEW_START
for FILENAME in $IN_NAME*.$IN_EXT; do
   mv "$FILENAME" temp_$IN_NAME$(printf %04d $COUNTER).$IN_EXT  
   let COUNTER++     #increment COUNTER for the next file
done

# Replace temp with the original file name
COUNTER=$NEW_START
for FILENAME in temp_$IN_NAME*.$IN_EXT; do
   mv "$FILENAME" $IN_NAME$(printf %04d $COUNTER).$IN_EXT  
   let COUNTER++     #increment COUNTER for the next file
done

fi

UpReply 
StartStopPage 1 of 1StopEnde