On my Ubuntu system, I commonly need to do a 'Deep Replace' in file contents, the file name and directory name. For example when copying source code as a template for another application.
I've put together a function in ~/.bashrc
which works, but fails if the find or replace string has white space in it. I believe this is due to the sed command not accepting white space in the script as is, and the cd to the path variable also fails where the path includes white space.
The arguments are ($1) directory, ($2) find text, ($3) replace text.
Is it possible to improve this script so the arguments can all include white space?
deepreplace() {
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
then
echo "A parameter is missing"
else
cd $1
vfind=$2
vreplace=$3
# Replace the string in file
find . -type f -exec sed -i "s/$vfind/$vreplace/g" {} +
# Replace the string in file names, then directories
find . -type d -name "*$vfind*" | while read f; do mv $f $(echo $f | sed "s/$vfind/$vreplace/g"); done
find . -type f -name "*$vfind*" | while read f; do mv $f $(echo $f | sed "s/$vfind/$vreplace/g"); done
fi
}
vfind=foo
andvreplace=bar
and you have a directory calledfoo
which is a subdirectory of another called foo (/whatever/foo/something/foo
). Do we need to be able to handle such cases?