Problem :
I want my web application to be free software, with a GNU license. I’ve read that I should add my name and the license in every file. I have a lot of files, so I guess that I could execute a command to write those lines to all the files. I’ve been looking around and found the sed
command to insert text. So I’d do:
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.nnThis 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.nnYou 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*/ ' ./*
but this command isn’t recursive, so I should do:
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.nnThis 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.nnYou 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*/ '
It should insert those lines into the first lines of every file (expect hidden ones that begin with a dot), but it’s not working.
What could I be doing wrong?
Should I really add this lines to EVERY file?
Solution :
Your solution works (copied and pasted into my shell), except that it also touches dotfiles in subdirectories. To avoid touching those, you need to invoke find
as
find -type f ! -name '.*' | xargs sed -i '1 i/* ... */'
(-and
is assumed between -type f
and ! -name '.*'
). Note that I need to escape !
in my shell (Bash).
Note that the insert
option of sed
doesn’t work if the files are blank. If you write some text in the current folder’s files, it works.
Apart from the configuration files, you should avoid writing to images. My guess is that it would be better to specify the formats to be written. Something like this:
find ./* -regex ".*.(php|js|txt|html|css|yml|twig)" -type f | xargs sed -i '1 i/* CCCopyright 2013 Manolo Salsas [BnThis 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.nnThis 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.nnYou 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*/ '
Update:
Another solution to filter files based on file types is to use ack‘s file type selection feature. Assuming, for instance, you’d want to select C and Perl files only:
ack -f --type=cc --type=perl | xargs sed -i '1 i/* ... */'
(type=cc
stands for C)