To change all occurences of the string foo to bar in all files in a directory I use

sed -i -- 's/foo/bar/g' *

Found that here

But I also want to change NAME to name etc. I tried,

sed -i -- 's/foo/bar/g' * ; sed -i -- 's/NAME/name/g' *

This creates new files. How can I get this command to rewrite the original file with foo replaced with bar and also NAME replaced with name and then there would be other replacements, without creating new files?


1 Answer 1


You can give several expressions to sed in one invocation:

sed -e 'expr' -e 'expr' -e ...

In your case:

sed -e 's/foo/bar/g' -e 's/NAME/name/g'

The expressions will be applied to each line of input in succession, from left to right.

  • Thank you that is exactly what I was looking for. thanks again
    – ofey
    Sep 5, 2017 at 9:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .