Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.\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*/ ' ./*

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.\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*/ '

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?

share|improve this question
    
What does not working mean? What happens if you execute the second command? Does the first command work as expected? Which OS are you using? –  Dennis Nov 8 '13 at 22:38
add comment

1 Answer 1

up vote 4 down vote accepted

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  [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*/ '

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)

share|improve this answer
    
Doesn't work for me. –  Manolo Nov 8 '13 at 14:10
    
There is an other issue. If you have images in that folder, those images will breake. You should include the desired formats to be written. –  Manolo Nov 9 '13 at 11:57
    
@ManoloSalsas Please don't put questions into answers. Whether or not you should add this line to every file depends on what the license requires. Usually it should go in every text-based source file. –  slhck Nov 9 '13 at 13:58
    
@slhck - Thank you very much. I'll take into account. –  Manolo Nov 9 '13 at 14:06
    
@Raystafarian The "garble" you edited out is the license the OP needs to add to every file. Please don't remove it. –  slhck Nov 9 '13 at 14:11
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.