#!/bin/sh # # fcatalogue # ---------- # # Renaming multiple files by matching patterns. # # Script by Paul Kremer # License: GPL (http://www.gnu.org/copyleft.html) # #Changelog: # # 0.1 initial release (feb 2001) # 0.11 bugfix release (june 2001) : # fixes a bug when cataloguing files with spaces in filenames # VERSION="0.11" #default options VERBOSE="0" #end default options function help { echo "$0 version $VERSION License: GPL Author: Paul Kremer" echo "" echo "Usage: `basename $0` [OPTION] 'PATTERN' new_file_prefix" echo "" echo "Options:" echo " -h --help displays this message" echo " -v --verbose displays live renaming status" echo "" echo "This shell-scipt can be used to cataloguize many files with different" echo "names. It uses the tools mv, awk and sed for renaming." echo "" echo "It renames all files matchong the specified pattern with the given" echo "prefix, with an index number added." echo "" echo "The pattern _must_ be specified inside parentheses on the command-line," echo "because the pattern is only interpreted by sh during execution of the" echo "script! The existing file-extensions (.*) will be preserved. If a file" echo "has no extension, it will remain without extension (the extension is" echo "set to the substring following the last occuring dot in a filename) ." exit 0 } if [ "$1" == "-h" ] then help fi if [ "$1" == "--help" ] then help fi if [ "$1" == "-v" ] then VERBOSE="1" fi if [ "$1" == "--verbose" ] then VERBOSE="1" fi if [ "$VERBOSE" -ne "1" ] then if [ $# -ne 2 ] then echo "Usage: `basename $0` [OPTION] 'PATTERN' new_file_prefix" echo " `basename $0` --help for more information" exit 1 else pattern=$1 new_file_prefix=$2 fi else if [ $# -ne 3 ] then echo "Usage: `basename $0` [OPTION] 'PATTERN' new_file_prefix" echo " `basename $0` --help for more information" exit 1 else pattern=$2 new_file_prefix=$3 fi fi RUNNER=0 for filename in $pattern # Traverse list of files matching pattern do #check if the filename has an extension EXTENSION_CHECK="" EXTENSION_CHECK=`echo $filename | sed -n /\\\./p` if [ "$?" != "0" ]; then echo "sed error" exit 1 fi #if the var EXTENSION_CHECK has sth. then preserve extension if [ "$EXTENSION_CHECK" == "" ]; then # if there is NO extension! NEWFILENAME=$new_file_prefix$RUNNER else # if there is an extension NEWFILENAME=$new_file_prefix$RUNNER.`echo $filename | awk -F "." '{ print $NF }'` if [ "$?" != "0" ]; then echo "awk error" exit 1 fi fi #verbosity if [ "$VERBOSE" == "1" ]; then echo "$filename ----> $NEWFILENAME" fi #now move the files mv "$filename" "$NEWFILENAME" #increment the index-number RUNNER=$(($RUNNER+1)) done if [ "$VERBOSE" == "1" ]; then echo "$RUNNER files renamed" fi exit 0