Replace All Instances of a String With Sed

Sed, a stream editor is very powerful and can be used to quickly replace all occurrences of a string within a string or file. Read intro to sed to get up to speed. This type of replacement is useful to quickly refactor or simply rename items in a large codebase right in the terminal.

First, you define the type of file you want to search for the target file in. Then you take those files found and search for a target string and set the string you want to replace the target file with.

find . -type f -name "*.ts" -print0 | xargs -0 sed -i '' -e "s/very/extremely/g"

If the strings have slashes in them be sure to escape them!

find . -type f -name "*.ts" -print0 | xargs -0 sed -i '' -e "s/all\/shared\/dist\/enums/all\/shared\/dist\/enum/g"

For convenience you can make this into a bash alias which we’ve talked about in bashrc for terminal

replaceInFiles () {
	find . -type f -name "$1" -print0 | xargs -0 sed -i '' -e "s/$2/$3/g"
}

# for example
> replaceInFiles '*.md' 'khaliq' 'khaliq-gant'

Read more about it here. This guide is a good tutorial as well.