#!/bin/bash -x # Proper header for a Bash script. RecurseWithTypes(){ DIR=$1 RecurseFiles "$DIR" "*.JPG"; } RecurseFiles(){ DIR=$1 type=$2 # save and change IFS OLDIFS=$IFS IFS=$'\n' # read all file name into an array local fileArray=($(find . -maxdepth 1 -type f -name "$type")) # restore it IFS=$OLDIFS # get length of an array local tLen=${#fileArray[@]} # use for loop read all filenames for (( i=0; i<${tLen}; i++ )); do local DIRNAME=$(echo "${PWD##*/}" | sed 's/ /_/g') echo "dirname $DIRNAME" local FILE=${fileArray[$i]}; echo "filename $FILE" convert -negate $FILE $FILE done } echo "Recursing folder"; STARTINGDIR=$PWD; RecurseWithTypes "$STARTINGDIR"