How to use sed to find and replace text in files in Linux / Unix shell

Originally published at:

I am a new Linux user. I wanted to find the text called “foo” and replaced to “bar” in the file named “hosts.txt.” How do I use the sed command to find and replace on Linux or UNIX-like system?

Hi there, I’m wondering why you use the -i flag without specifying a suffix/extension? The man page suggests this makes it useless to use -i.

Thanks for the explainer. I’m a very basic command line user so the level this is written at is good for me - I can make sense of almost all of it :slight_smile:

The -i option passed to the sed command update file. a suffix/extension is optional. But you provide one a backup file will be created.