Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Created October 18, 2013 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmbuko/7044590 to your computer and use it in GitHub Desktop.
Save pmbuko/7044590 to your computer and use it in GitHub Desktop.
A shell script that recursively finds and replaces usernames inside files within a directory.
#!/bin/bash
workpath=/opt/auth_files/
outfile=/root/auth_find-$1
# friendly usage funtion, called if no argument is supplied
usage ()
{
cat<<EOF
Usage: auth_find [username] [new-username]
This script recursively searches subversion's /opt/auth_files/ directory for
the supplied username and returns a list of files that contain it. If a second
username is supplied all instances of the first will be replaced with the second.
Output is sent to both STDOUT and /root/auth_find-username.
EOF
exit 1
}
if [ $# == 1 ]; then # do this block if one argument is given
echo "Results:"
for afile in $(tree -ifF --noreport $workpath | grep -v '/$'); do
if [ -n "$(grep "^$1 " $afile)" ]; then
echo "$afile" | tee -a $outfile
fi
done
else
if [ $# == 2 ]; then # do this block if two arguments are given
echo "Now replacing occurrences of '$1' with '$2' in the following files:"
for afile in $(tree -ifF --noreport $workpath | grep -v '/$'); do
if [ -n "$(grep "^$1 " $afile)" ]; then
echo "$afile" | tee -a $outfile-CHANGED
perl -p -i -e "s|$1|$2|g" $afile
fi
done
else # show usage if incorrect number of arguments given
usage
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment