views:

185

answers:

3

I do not want:

$ cat file > dummy; $ cat header dummy > file

I want similar to the command below but to the beginning, not to the end:

$ cat header >> file
+5  A: 

You can't append to the beginning of a file without rewriting the file. The first way you gave is the correct way to do this.

Mark Byers
A: 

Thanks to right searchterm!

echo "include .headers.java\n$(cat fileObject.java )" > fileObject.java

Then with a file:

echo "$(cat .headers.java)\n\n$(cat fileObject.java )" > fileObject.java
HH
That contains a race condition; sometimes (if not always), the shell will set up your stdout redirection, which will zero out the file, before the second "cat" command is complete.
pra
pra: fixed? $ echo "$( read -t 1; cat one) \n\n $(cat two)" > one?
HH
+1  A: 

You can't prepend to a file without reading all the contents of the file and writing a new file with your prepended text + contents of the file. Think of a file in Unix as a stream of bytes - it's easy to append to an end of a stream, but there is no easy operation to "rewind" the stream and write to it. Even a seek operation to the beginning of the file will overwrite the beginning of with any data you write.

rlotun