Adding a line to the beginning of a file in Unix/Linux

Posted by brian | Posted in Shell Programming | Posted on 05-11-2009

0

This morning someone asked me how to do a relatively simple operation from a Unix shell: Adding a line of text to the beginning of a file.

Much to my surprise he asked for help with amazingly complex methods he had tried.

Okay everyone, no need to hit this thumbtack with a sledgehammer!  Let’s say you have a file “textfile.txt” and want to put “<Header>” on the top:

echo -e “<Header>\n$(cat textfile.txt)” > textfile.txt

There you go, nice and simple.