Skip to content

Instantly share code, notes, and snippets.

@shortdudey123
Created July 28, 2014 07:53
Show Gist options
  • Save shortdudey123/431b3787f5b7a3a10b94 to your computer and use it in GitHub Desktop.
Save shortdudey123/431b3787f5b7a3a10b94 to your computer and use it in GitHub Desktop.
Change filename and update words in file
#!/bin/bash
echo "What directory would you like to execute this script on?"
echo "NOTE: do not execute this on the same directory that contains this script"
read FILEPATH
for file in $(echo $FILEPATH*); do
filenew=`echo $file | sed 's/foo/bar/g'`
if [ "$filenew" != "$file" ]
then
mv $file $filenew
echo "$file was moved to $filenew"
fi
fooCount=$(grep -c foo $filenew)
if [ "$fooCount" -ne "0" ]
then
sed -i 's/foo/bar/g' $filenew
echo "Change $filenew in $fooCount places"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment