我希望我的 Web 应用程序是免费软件,并具有 GNU 许可证。我读到过我应该在每个文件中添加我的名字和许可证。我有很多文件,所以我想我可以执行一个命令将这些行写入所有文件。我一直在寻找并找到了sed
插入文本的命令。所以我会这样做:
sed -i '1 i\/* Copyright 2013 Manolo Salsas \nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.\n\nThis 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.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/ ' ./*
但这个命令不是递归的,所以我应该这样做:
find ./* -type f | xargs sed -i '1 i\/* Copyright 2013 Manolo Salsas \nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.\n\nThis 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.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/ '
它应该将这些行插入到每个文件的第一行(期望以点开头的隐藏行),但它不起作用。
我可能做错了什么?
我真的应该将这行添加到每个文件中吗?
答案1
您的解决方案有效(复制并粘贴到我的 shell 中),但它还涉及子目录中的点文件。为了避免触及这些文件,您需要find
调用
find -type f \! -name '.*' | xargs sed -i '1 i\/* ... */'
(-and
假设在-type f
和之间\! -name '.*'
)。请注意,我需要!
在我的 shell(Bash)中转义。
请注意,如果文件为空,则insert
选项sed
不起作用。如果您在当前文件夹的文件中写入一些文本,则该选项有效。
除了配置文件之外,您应避免写入图像。我猜最好指定要写入的格式。像这样:
find ./* -regex ".*\.\(php\|js\|txt\|html\|css\|yml\|twig\)" -type f | xargs sed -i '1 i\/* CCCopyright 2013 Manolo Salsas [B\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.\n\nThis 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.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/ '
更新:
根据文件类型过滤文件的另一种解决方案是使用确认的文件类型选择功能。例如,假设您只想选择 C 和 Perl 文件:
ack -f --type=cc --type=perl | xargs sed -i '1 i\/* ... */'
(type=cc
代表 C)