Use sed to edit files in place (and make a backup copy)

Yesterday I ran into a situation where I had to edit 250,000 files, and of course I instantly thought of the Unix/Linux sed command. I knew what edit commands I wanted to run (simple swap/replace commands), but my bigger problem was how to edit the files in place.

A quick look at the sed man page showed that I needed to use the -i argument of the sed command:

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)

Since I did want to make a backup of each file, I included a filename extension, so my sed command looked a little like this example:

sed -i.bak -e's/2011/2012/' $filename

Note that I used the filename extension ".bak", and not just "bak". (You have to include the decimal if you want it, and I wanted my files to be named something like "foo.html.bak", not "foo.htmlbak".)

My full sed "edit files in place" example

I just showed that example so you could see something simple. In reality what I did was to run a shell script that looked like this:

#!/bin/sh

# create 'html_files.txt' like this:
# find . -type f -name "*.html" > html_files.txt

for file in `cat html_files.txt`
do
  sed -i.bak -f my_commands.sed $file
done

This shell script:

  1. Reads from a file name html_files.txt.
  2. For every filename in that file, this script executes the sed command shown.
  3. Like the earlier example, this sed command makes a backup copy of each file it works on, adding the '.bak' extension to the filename.
  4. The sed command executes the commands contained in the file my_commands.sed.

To help understand this example better, the sed commands contained in the file my_commands.sed looked like this:

s|<TITLE>|<title>|
s|foo|bar|

(They were just a collection of sed swap/replace commands that I needed to run on every file.)

How to use sed to edit files in place - Summary

I hope these examples of how to use the Unix/Linux sed command to edit files in place has been helpful. For more information, take a look at the sed man page, or leave a note in the Comments section below.

Post new comment

The content of this field is kept private and will not be shown publicly.