Quick Replace

This one is going to be short, it is something that would fit better under commandlinefu, tips and tricks!

What do we want to do?

Look for al the cpp files under a directory and modify all the text with foo to bar.

How to do it?

First we find all the cpp files under the current directory
find . -name "*.cpp" -print

And a possible command to replace from a file is using sed.
sed -i 's/foo/bar/g'

We just have to connect those two together to replace in all the files with a name ending in .cpp, that’s what pipes are for.
find . -name "*.cpp" -print | xargs sed -i 's/foo/bar/g'



Leave a comment

November 1, 2014 · 6:35 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.