sed – match and replace BIND/DNS serial number programmatically

I had to make modifications to several BIND zone files. Sed proved itself invaluable once again.

The zone files looked like this:

domain.com. IN SOA ns1.domain.com. postmaster.domain.com. (
               2015021001   ; Serial
               600          ; Refresh (10 minutes)
               1800         ; Retry (30 minutes)
               3600000      ; Expire (1000 hours)
               3600         ; Minimum (1 hour)
               )

To update the serial number on every zone file at once:

sed -i 's/[0-9]\{10\}\s*; Serial/2016020101   ; Serial/' *.zone

This matches a 10-digit number followed by optional whitespace and “; Serial”, then replaces it with the new serial. The -i flag edits files in place — test without it first to see what would change.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *