Created
October 18, 2013 17:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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