How to Add Text to the Beginning of File in Linux

Linux command line users are familiar with the concept of redirection operators, which can write output to a file, or read input from a file.

The operator '>' writes output to a new file, or to an existing file; in which case it wipes off everything which is previously present in the file. The operator '>>' on the other hand appends output at the end of an existing file. However, there is no dedicated operator to write new text to the beginning of a file.

Today, we will see two ways to add text to the beginning of a file in Linux: one with a workaround, and one with a command.

Note: It is a good approach to first backup the original file and then append the new text to the original file. This way if any error occurs while using the commands, we can always restore the backup file. Eg. You can simply copy a file called ‘file1’ to another file called ‘file1_bkp’ and then append the text to the original, i.e. ‘file1’.

Add Text to Beginning of File Using echo and cat Commands

As you might know, the echo command prints whatever is passed as the argument to the output, whereas the cat command prints the contents of a file to the output.

We can thus pass the new text to be added to echo, pass the file to a cat, combine and write the output to the file. This can be done as follows:

$ x=`echo "New Text"; cat filename`
$ echo "$x" > filename

Basically, we are combining the new text and existing contents of the file, and storing the new contents in a variable. Then, we simply output the variable and redirect it to the same file.

$ cat samplefile.txt
$ x=`echo "Name: John"; cat samplefile.txt`
$ cat samplefile.txt
Redirect Variable to File
Redirect Variable to File

As you can see above, the new text was added on a new line at the beginning of the file. To add the text at the beginning of the existing first line, use the -n argument of echo.

$ x=`echo -n "New Text"; cat filename`
$ echo "$x" > filename
Add Text to Beginning of File
Add Text to Beginning of File
Note that although Bash or many other shells do not have a limit on the size of a variable, however, the size may be limited based on the environment and system configuration. Hence there might be a scenario when ‘samplefile.txt’ is a file containing a humongous amount of data, and the variable fails to store all of it.

Add Text to First Line of File Using sed Command

sed (short for ‘Stream Editor‘) is a command-line editor in Linux, that is used to perform operations like writing text to a file, find and replace, etc.

sed is a complex command with a lot of options to edit files in various ways. We will be using particular features of sed to add text to the beginning of a file.

Run the following to insert text on a new line at the beginning.

$ sed -i '1i New Text' filename

The -i argument tells sed to modify the file itself. Without it, sed automatically creates a copy of the file and performs all modifications on the copy.

The string ‘1i New Text’ means: ‘insert’ the string ‘New Text’ on the ‘1st’ line.

$ cat samplefile.txt
$ sed -i '1i Introduction' samplefile.txt
$ $ cat samplefile.txt
Insert Text New Line at Beginning of File
Insert Text New Line at Beginning of File

Again, this is adding the new content on a new line at the beginning. To add new text on the existing line, run:

$ sed -i '1s/^/New Text/' filename

Basically, the exponent symbol (‘^’) signifies a null string. The '1' at the beginning signifies that sed should consider the null string at beginning of line 1 and the 's' is a sed command, to replace a string. Here, the null string is replaced by ‘New Text‘.

$ cat samplefile.txt
$ sed -i '1s/^/Person/' samplefile.txt
$ cat samplefile.txt
Insert Text to Exsiting Line in File
Insert Text to Existing Line in File
Conclusion

We learned two simple yet catchy ways to add new text to the beginning of a file in Linux. Of course, the obvious way to do this, if it is to be done manually, is to modify the file with a file editor. However, in a script, the methods discussed here can be used.

Thanks for reading and let us know your thoughts in the comments below!

4 thoughts on “How to Add Text to the Beginning of File in Linux”

  1. The first variant has a limit on the file size and that limit could be much smaller than the file system’s limit on file size.

    We can’t store unlimited data in the variables. I think you should mention it.

    Reply
  2. A better and safer solution:

    1. Create a new temp file.
    2. Write the new text to the start of the new temp file.
    3. Append the full text from the original file to the new temp file.
    4. Delete the original file, or better still, rename the original file to a backup filename.
    5. Rename the temp filename to the original filename.

    Sometimes safety is better than efficiency. If any errors occurred, with the examples you posted, you could lose the entire original file. Especially when you failed to advise the reader to back up the original file before performing the “insert” directly into the original file!

    Reply

Got something to say? Join the discussion.