Skip to main content
Added OS information
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

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~/.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
}

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
}

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
}
Source Link
gbro3n
  • 121
  • 5

Bash function for 'Deep Replace' in file contents, file name, directory name, allowing for whitespace

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
}