#!/bin/bash -u ############################################################################# # FILE-MV-ALL, a function that expands the shell 'mv' command to accept to # rename multiple files with similar names. # Usage: # file-mv-all TAG NEWTAG [-exclude= -exclude= ] [ -passive ] # Selects all files that match regex *TAG* and rename them to *NEWTAG* # Additional tags can be provided to exclude files from being renamed. # Optional input flag defines the interactive nature of the function, # By default FILE-MV-ALL displays list of files and changes that will be made # If the '-passive' or '-p' flag is provided then no output is given and # changes are immediately executed # #@ Created by Pedro Inacio ############################################################################# HELP="FILE-MV-ALL, a function that expands the shell 'mv' command to accept to \n rename multiple files with similar names.\n Usage:\n \tfile-mv-all TAG NEWTAG [-exclude= -exclude= ] [ -passive ]\n\n Selects all files that match regex *TAG* and rename them to *NEWTAG*\n Additional tags can be provided to exclude files from being renamed. Optional input flag defines the interactive nature of the function,\n By default FILE-MV-ALL displays list of files and changes that will be made\n If the '-passive' or '-p' flag is provided then no output is given and \n changes are immediately executed." # TODO: Add support for subfolder renaming. # TODO: Add support for excluding patterns. # Parameters CMD='mv' #CMD='echo mv' #DEBUG mode #Verify inputs case $# in 0) echo -e ERROR: Invalid number of arguments. echo -e $HELP exit 3 ;; 1) if [ $1 == "--help" ] then echo -e $HELP exit 0 else echo -e ERROR: Invalid argument, $1 echo -e $HELP exit 3 fi ;; *) #Two or more input arguments TAG=$1; NEWTAG=$2; MODE=active #Iterate through rest of inputs i=2 ARGLIST=($@) EXCLIST= while [ $i -lt $# ] do ARG=${ARGLIST[$i]} #check for exclude inputs if [ ${ARG#-exclude=*} != $ARG ] then #echo Found exclude pattern, ${ARG#-exclude=*} EXCLIST=( ${EXCLIST[*]} ${ARG#-exclude=*} ) #check for passive mode flag elif [ $ARG == -passive -o $ARG == -p ] then #echo Found mode, passive MODE=passive #fail on unknown input else echo -e ERROR: Invalid argument, $ARG. echo -e $HELP exit 3 fi ((i++)) done ;; esac #check that any files in folder match TAG ls *$TAG* 1>/dev/null 2>&1 if [ $? == 0 ] then FILES=(*$TAG*) else echo No files in current folder matching expression: $TAG exit 0 fi # remove files that match any of the exclude tags EXCLFILES= for EXCLTAG in ${EXCLIST[*]} do IMAX=${#FILES[*]} for (( i=0 ; i < $IMAX ; i++ )) do FILE=${FILES[$i]} if [ ${FILE/$EXCLTAG/} != $FILE ] then EXCLFILES=(${EXCLFILES[*]} $FILE) unset FILES[$i] fi done #Refresh FILES array removing empty positions FILES=( ${FILES[*]} ) done #echo Excluded: ${EXCLFILES[*]} #echo Included: ${FILES[*]} if [ $MODE == active ] then #Test if EXCLFILES is empty if [ ${#EXCLFILES} -gt 0 ] then echo "List of excluded files," for f in ${EXCLFILES[*]} do echo -e "\t$f" done fi echo "The following files will be renamed," for f in ${FILES[*]} do echo -e "\t$f -> ${f/$TAG/$NEWTAG}" done #Get user reply echo "" ANSWER=EMPTY while [ $ANSWER != Y -a $ANSWER != y -a $ANSWER != yes ] do echo -e "Do you wish to continue and rename files? [Y]es or [N]o?" read ANSWER if [ $ANSWER == N -o $ANSWER == n -o $ANSWER == no ] then exit 0 fi done fi #rename files for f in ${FILES[*]} do $CMD $f ${f/$TAG/$NEWTAG} done exit