Add headers recursive on top of each code file
Posted #CaffeeLog#Linux
In various situations during the work on a project, it could happen that one is required to add a header to a lot of source code files.
Since programmers are usually lazy — eh, interested to work efficiently — they won’t open every file and copy it manually. Once again, the Bash can help us.
For example, a copyright notice in all C/C++ files:
find . -name \*.{c,cpp,h,hpp} | xargs sed -i "1i/*Copyright .... you can also use newlines, tabs etc....\n\t..*/"
Or, to add a import statement
in all python files:
find . -name \*.py | xargs sed -i "1iimport fancy.new.packet"
Important in the argument for sed
is the leading 1i
, which means:
1
start in first linei
insert directly at the beginning of the line. In contrast,1a
would append at the first line.
The combination of find
and xargs
executes the command recursive in the current directory.
The \*.XYZ
is a RegEx to match the correct file types.
Based on helpful posts on Stackoverflow.