Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
How can I prepend a tag to the beginning of several files?

Other than create a temporarily file with a line at the heading, and move it back when finished, is there a standard way to prepend some lines to a file?

i.e sometimes you do (echo #GPL license; cat $file) > tmpfile; mv tmpfile $file

EDIT2

In the first place, I was just trying to see if there's a standard command that ships with common distros, I guess not

EDIT

And notice that the source to prepend might not just be a fixed string, it could be a file as well, i.e cat $header $file > tmpfile; mv tmpfile $file

share|improve this question

marked as duplicate by Gilles, jasonwryan, Mat, Ulrich Dangel, Renan Nov 29 '12 at 12:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
what do you mean by "line at head"? –  Guru Nov 28 '12 at 11:57
    
@Guru i.e (echo #GPL license; cat $file) > tmpfile; mv tmpfile $file –  warl0ck Nov 28 '12 at 12:04
1  
In case writing to tmpfile fails for some reason, use && mv ... instead of ; mv ... -- that will keep $file from being overwritten with "bad" content. –  glenn jackman Nov 28 '12 at 15:45

1 Answer 1

If sed(GNU) is ok for you :

$ sed -i '1i #GPL License' file

In case of the source being a file:

The source files:

$ cat file1
hi
hello
$ cat file2
welcome to
Unix SO.

The sed command:

$ sed -i  '1{
r file1
h;d
};2{H;g;}' file2

The output after the sed command :

$ cat file2
hi
hello
welcome to
Unix SO.

The 'r' command of sed cannot read a file before the 1st line, and hence this solution. The 1st line is kept in hold memory, the contents of file1 are sent to terminal, then when the second line comes, it is printed together with 1st line.

share|improve this answer

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