Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jun 212010

Replace infile using sed and make a backup automatically


If you use the -i flag in sed, it will replace infile, which is quite dangerous if you don't have a backup.


# sed -i 's/foo/bar/gi' input.txt

To auto make a backup when replacing in a file, you can use

# sed -i.bak 's/foo/bar/gi' input.txt

Now the original file will stored as input.txt.bak in the same directory that you are working.

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>