GNU sedを使うと、複数のファイルを一度に安全に変更できます。
ソースコードファイルの先頭に1行を追加します。
sed -i '1i/* Copyright (C) FOO BAR */' *.c
テキストに「\n」を使えば、数行を追加することもできます。
sed -i '1i/*\n * Copyright (C) FOO BAR\n * Created by Jane Doe\n */' *.c
ヘッダーには、FOO BARの著作権表記と「作成者: Jane Doe」という行を追加します。
別のファイルから複数行を追加するには、0rFILEを使います。典型的な用途は、すべてのファイルにライセンス表記のヘッダーを追加することです。
## 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 <https://www.gnu.org/licenses/>.
*/
EOF
## Add the file at the beginning of all source code files:
$ sed -i '0rLIC.TXT' *.cpp *.h
ヘッダーファイルを作成します。ヘッダー内のライセンス表記の訳は次のとおりです。Copyright (C) 1989–2021 FOO BAR。このプログラムは自由ソフトウェアです。Free Software Foundationが公表するGNU General Public Licenseのバージョン3、または任意のそれ以降のバージョンの条件に従って、再配布または変更できます。このプログラムは役立つことを期待して配布されていますが、いかなる保証もありません。商品性や特定目的への適合性についての黙示の保証さえありません。詳しくはGNU General Public Licenseを参照してください。このプログラムとともにGNU General Public Licenseの写しを受け取っているはずです。そうでなければ、https://www.gnu.org/licenses/ を参照してください。 その後、このファイルをすべてのソースコードファイルの先頭へ追加します。
スクリプトファイル(たとえば、.sh、.py、.plファイル)では、通常、ライセンス表記は最初の行(シバン「#!」の行)の後に置きます。1rFILEコマンドは、最初の行の後にFILEを追加します。
## 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 <https://www.gnu.org/licenses/>. ## ## EOF ## Add the file at the beginning of all source code files: $ sed -i '1rLIC.TXT' *.py *.sh
ヘッダーファイルを作成します。ヘッダー内のライセンス表記の訳は次のとおりです。Copyright (C) 1989–2021 FOO BAR。このプログラムは自由ソフトウェアです。Free Software Foundationが公表するGNU General Public Licenseのバージョン3、または任意のそれ以降のバージョンの条件に従って、再配布または変更できます。このプログラムは役立つことを期待して配布されていますが、いかなる保証もありません。商品性や特定目的への適合性についての黙示の保証さえありません。詳しくはGNU General Public Licenseを参照してください。このプログラムとともにGNU General Public Licenseの写しを受け取っているはずです。そうでなければ、https://www.gnu.org/licenses/ を参照してください。 その後、このファイルをすべてのソースコードファイルの先頭に追加する例を示します。
上のsedコマンドは、findと組み合わせて全サブディレクトリ内のファイルを探し、xargsで選択したファイルに追加のコマンドを実行し、grepで著作権表記をすでに含むファイルを除外できます。
find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) \
| xargs grep -Li copyright \
| xargs -r sed -i '0rLIC.TXT'
または、スペースや改行を含むファイル名を扱う、少し安全性を高めた書き方もあります。
find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) -print0 \
| xargs -0 grep -Z -Li copyright \
| xargs -0 -r sed -i '0rLIC.TXT'
注: rコマンドで0アドレスを使うには、GNU sedのバージョン4.9以降が必要です。ゼロアドレスを参照してください。