#!/bin/bash # # multi-file rename script for Nautilus # (c) Andrey Yurovsky http://andrey.thedotcommune.com/ # # To install, place this script into your .gnome2/nautilus-scripts/ directory, # which is located in your home directory. 'rename' should appear in your # 'scripts' menu when you restart Nautilus (or log out and then log in again). # # Select one or more files in Nautilus, right-click one of the selected files, # and choose 'rename' from the 'scripts' menu. A dialog box will ask for a # new name. Enter a new name, with a starting index in square brackets # (ex: [1]). The selected files will be renamed to this new name, incrementing # the index each time. For example, if the new name is "photo[1].jpg", the # files will be renamed "photo1.jpg", "photo2.jpg", and so on. # ask for a name pattern PATTERN=`zenity --entry --title="Rename Files" --text=name:` if [ "${#PATTERN}" = "0" ]; then exit fi # check that the pattern looks valid if [ "`echo $PATTERN | egrep [^\[][\[][0-9]+[\]][^\]]`" != $PATTERN ]; then zenity --error --text="The name should contain a number in square brackets, for example 'photo[1].jpg' is valid." exit fi # extract the starting index BNUM="`echo $PATTERN | awk '{printf( "%d-%d", M=match( $1,/[\[]([0-9]+)[\]]/ )+1, RLENGTH+M-3 );}'`" NUM=`echo $PATTERN | cut -b $BNUM` # extract the left and right parts of the new name let L=${BNUM%-*}-2 let R=${BNUM#*-}+2 LEFT=`echo $PATTERN | cut -b 1-$L` RIGHT=`echo $PATTERN | cut -b $R-` # rename each file for FILE in "$@"; do mv "$FILE" "$LEFT$NUM$RIGHT" let NUM+=1 done