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.
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
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 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
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
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!
I tested the echo and cat examples but could only get it to sort of work by using back-ticks instead of single quotes when assigning the x variable. The resulting file had all of the new-line characters removed and displayed it on a single line.
I test this in bash on Linux Mint 20.1.
A better and safer solution:
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!
Hi Rick,
Thanks for the suggestion. We will include a note to advise the user to backup the file first.
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.
Hi Michael,
You are right, there could be a limit on the size of variables. We will include this in the article.