Differences between revisions 3 and 4
Revision 3 as of 2008-11-22 14:09:11
Size: 449
Editor: localhost
Comment: converted to 1.6 markup
Revision 4 as of 2008-11-22 23:33:03
Size: 515
Editor: GreyCat
Comment: first-line and clean up of English
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
You cannot do it with bash redirections alone; the opposite of `>>` does not exist....
Line 4: Line 5:
You cannot do it with bash redirections only, ie the opposite of >> does not exist.

You can use an editor:
To insert content at the beginning of a file, you can use an editor:
Line 16: Line 15:
you can use things like:
Or
you can rewrite the file, using things like:
Line 22: Line 22:
or a gazillion of other solutions or lots of other solutions.

How do I prepend a text to a file (the opposite of >>)?

You cannot do it with bash redirections alone; the opposite of >> does not exist....

To insert content at the beginning of a file, you can use an editor:

ed -s file << EOF
1i
header line 1
header line 2
.
w
EOF

Or you can rewrite the file, using things like:

{ echo line; cat file ;} >tmpfile && mv tmpfile file
echo line | cat - file > tmpfile && mv tmpfile file

or lots of other solutions.

BashFAQ/090 (last edited 2017-02-14 19:48:12 by GreyCat)