Replace substrings found in files

There are times when you need to find some specific substring in files and replace them with something else. Let’s say, you want to find all occurrences of substring “foo” and replace them with “bar” in all java files under current directory.

Here’s how you can do it on Linux (cd to your target directory first):

find . -name "*.java" -exec sed -i "s/foo/bar/g" {} +

If you are using Mac, the command will be a bit different, here’s how you do it on Mac

find . -type f -name "*.java" -exec sed -i '' -e  "s/foo/bar/g" {} \;

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *