Adding a header to source files

Put the header content into a text file and load it into a variable:

cd ~/temp/
nano h.txt
h=$(<h.txt)

And run this command:

# Turn on recursive globbing and extended globbing and have patterns that
# fail to match expand to an empty string
shopt -s globstar extglob nullglob
for f in **/*.@(h|cpp); do
  # Add the contents of the variable h at the beginning of the file
  # Note: no lines in $h should be just a period as that signals end of
  # text input mode.
  ed -s "$f" <<EOF
1i
$h

.
w
EOF
done

it also adds missing newlines at the end of the files.

Leave a Reply

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