#!/bin/bash printHelp () { echo "Usage: $0 -f find -r replace FILES_TO_RENAME*" echo -e "\t-f The text to find in the filename" echo -e "\t-r The text used to replace with in the filename" exit 1 } while getopts "f:r:" opt; do case "$opt" in r ) replace="$OPTARG" ;; f ) fnd="$OPTARG" ;; ? ) printHelp ;; esac done shift $(( $OPTIND - 1 )) if [ -z $replace ] || [ -z $fnd ] then echo "Need a string to find and a string to replace"; printHelp fi for i in $@ do newname=$(echo $i | sed "s/$fnd/$replace/") mv $i $newname echo "Renamed file $i to $newname" done