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.

Replace infile using sed and make a backup automatically

Answer:

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

E.g.

# 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.

  1. How to perform hot backup for InnoDB
  2. Do not save backup copies of files when doing revert in Mercurial
  3. Replace multiple strings using sed
  4. How to replace a character for a new line in vi?
  5. Copy a directory (for backup) to another location

Leave a Reply

(required)

(required)

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>