#!/bin/bash

#---------------------------------------------------------------------------------------------------------------------------------------------#

# Purpose: To look through a directory and find all files that contain a string; given as Argument 1, and replace that string with Argument 2

# Author: Jonathan Laberge

# This script is free to use and can modify to suit your own needs

#---------------------------------------------------------------------------------------------------------------------------------------------#

#---------------------------------------------------------------------------------------------------------------------------------------------#

# Declaring Globals Variables

#---------------------------------------------------------------------------------------------------------------------------------------------#

VERSION = "Version: 1.0.1"

#--------------------------------------------------------------------------------------------------#

# Function usage

# Globals:

# CURTIME

# TEMPFILE

# Arguments:

# FILENAME

# Returns:

# None

#--------------------------------------------------------------------------------------------------#

function usage ( ) {

local OPTIND = 1

USAGE = " $(basename "$FUNCNAME") : STRING_TO_FIND REPLACE_WITH_STRING

or: -d STRING_TO_FIND REPLACE_WITH_STRING PATH_TO_DIR

Summary: This script browses $filePath for files that contain a specific string, and then replaces that string

for each file name that matches.

Arguments:

-d Enables depth, for searching directories within the filepath

-l Lists files in the filePath (excludes any non-file items)

-a Auto-mode; find files with yesterday's date and replace with today's

-h Print Help (this message) and exit

-v Print version information and exit"

filePath = '/path/to/file'

depth = "-maxdepth 1"

files = "Files currently in $filePath :"

findString =

replaceString =

while getopts "hvdla" opt; do

case $opt in

h )

echo " $USAGE " >& 2

exit 1

;;

a )

findString =$ ( date --date = "1 day ago" + '%d-%b' )

replaceString =$ ( date + '%d-%b' )

;;

v )

echo " $VERSION "

exit 1

;;

l )

echo $files

find $filePath $depth -type f | sed 's|' $filePath '/|./|'

exit 1

;;

d )

files = "Files currently in $filePath and containing folders:"

depth = "-depth"

;;

\? )

echo "Invalid option: - $OPTARG " >& 2

exit 1

;;

: )

echo "Option - $OPTARG requires an argument." >& 2

exit 1

;;

esac

done

shift $ ( ( $OPTIND - 1 ) )

# After options are taken into account, declare necessary variables for renameFiles()

# Options should be shifted and therefore allowing $1 to be first argument and not the option.

if [ -z $3 ] ; then #If 3rd argument is empty, it defaults to whatever is specified; else $3 will be file path

filePath = '/path/to/file'

else

filePath = "$3"

fi

if [ -z $findString ] && [ -z $replaceString ] ; then

findString = "$1"

replaceString = "$2"

fi

}

#--------------------------------------------------------------------------------------------------#

# Function renameFiles

# Requires 2 Arguments at least, this function searches a directory

# Globals:

# CURTIME

# TEMPFILE

# Arguments:

# FILENAME

# Returns:

# None

#--------------------------------------------------------------------------------------------------#

function renameFiles ( ) {

fileCount =$ ( find $filePath $depth -name "* $findString *" | wc -l )

find $filePath $depth -name "* $findString *" -execdir bash -c 'for f; do mv -iv "$f" "${f//' " $findString " '/' " $replaceString " '}"; done' bash { } +

echo " $fileCount File(s) have been renamed."

}

#---------------------------------------------------------------------------------------------------------------------------------------------#

# Executing functions

#---------------------------------------------------------------------------------------------------------------------------------------------#

if [ $# -ge 2 ] || [ $1 == '-a' ] ; then

usage "$@"

renameFiles

exit

else

if [ -z $1 ] ; then

echo "Missing Operands."

echo "Try ' $(basename "$0") ' --help for more information."

else

usage $1

fi