manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: Adding a header to multiple files,  Next: tac,  Prev: Line length adjustment,  Up: Examples

7.9 Adding a header to multiple files
=====================================

GNU ‘sed’ can be used to safely modify multiple files at once.

Add a single line to the beginning of source code files:

     sed -i '1i/* Copyright (C) FOO BAR */' *.c

Adding a few lines is possible using ‘\n’ in the text:

     sed -i '1i/*\n * Copyright (C) FOO BAR\n * Created by Jane Doe\n */' *.c

   To add multiple lines from another file, use ‘0rFILE’.  A typical use
case is adding a license notice header to all files:

     ## Create the header file:
     $ cat<<'EOF'>LIC.TXT
     /*
         Copyright (C) 1989-2021 FOO BAR

         This program is free software; you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation; either version 3, or (at your option)
         any later version.

         This program is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         GNU General Public License for more details.

         You should have received a copy of the GNU General Public License
         along with this program; If not, see .
     */
     EOF

     ## Add the file at the beginning of all source code files:
     $ sed -i '0rLIC.TXT' *.cpp *.h

   With script files (e.g.  ‘.sh’,‘.py’,‘.pl’ files) the license notice
typically appears _after_ the first line (the 'shebang' ‘#!’ line).  The
‘1rFILE’ command will add ‘FILE’ _after_ the first line:

     ## Create the header file:
     $ cat<<'EOF'>LIC.TXT
     ##
     ## Copyright (C) 1989-2021 FOO BAR
     ##
     ## This program is free software; you can redistribute it and/or modify
     ## it under the terms of the GNU General Public License as published by
     ## the Free Software Foundation; either version 3, or (at your option)
     ## any later version.
     ##
     ## This program is distributed in the hope that it will be useful,
     ## but WITHOUT ANY WARRANTY; without even the implied warranty of
     ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     ## GNU General Public License for more details.
     ##
     ## You should have received a copy of the GNU General Public License
     ## along with this program; If not, see .
     ##
     ##
     EOF

     ## Add the file at the beginning of all source code files:
     $ sed -i '1rLIC.TXT' *.py *.sh

   The above ‘sed’ commands can be combined with ‘find’ to locate files
in all subdirectories, ‘xargs’ to run additional commands on selected
files and ‘grep’ to filter out files that already contain a copyright
notice:

     find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) \
         | xargs grep -Li copyright \
         | xargs -r sed -i '0rLIC.TXT'

Or a slightly safe version (handling files with spaces and newlines):

     find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) -print0 \
         | xargs -0 grep -Z -Li copyright \
         | xargs -0 -r sed -i '0rLIC.TXT'

   Note: using the ‘0’ address with ‘r’ command requires GNU ‘sed’
version 4.9 or later.  *Note Zero Address::.

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.