Adding a Line to the Top of a File with Sed
August 25th, 2009
I recently needed to add a copyright notice to the top of multiple Java files. In the directory where the files were located I ran the command
sed -e '1i\/* Copyright (C) 2009 Aaron Gember. */\n' -i *.java
The -e option tells sed to execute the “script” in single quotes ('). The script is the line in which to insert (1 to insert at the time), the command to insert (i\), the text to insert (/* Copyright (C) 2009 Aaron Gember. */), and a newline to add a blank line (\n). The -i options tells sed to edit the files in place - i.e. change the existing files, don’t create new ones. The files to change are all Java files (*.java).