1. Overview
In Linux, we can use several command-line utilities, such as cat, sed, awk, tee, and so on, for displaying and modifying text files.
In this tutorial, we’ll learn how to append text at the beginning of a file in Linux.
2. Scenario Setup
Let’s start by taking a look at our text file named content.txt:
$ cat content.txt
content: line-1
content: line-2
Now, let’s imagine that our goal is to append the text from the header.txt file at the beginning of the content.txt file:
$ cat header.txt
header: line-1
header: line-2
Lastly, we must remember not to modify the header.txt file.
With this setup in place, let’s get started.
3. Using cat
We can concatenate content from multiple files using the cat command:
$ cat header.txt content.txt
header: line-1
header: line-2
content: line-1
content: line-2
Since our goal is to have this output in the existing content.txt file, let’s redirect the output to an intermediate temporary file:
$ cat header.txt content.txt > content.txt.tmp
Lastly, let’s replace the content.txt with the temporary file:
$ mv content.txt.tmp content.txt && cat content.txt
header: line-1
header: line-2
content: line-1
content: line-2
Great! We’ve verified that the content.txt file contains the desired result.
4. Using awk
We can also write an awk one-liner command to concatenate the header.txt and content.txt files:
$ awk '{print}' header.txt content.txt
header: line-1
header: line-2
content: line-1
content: line-2
Like earlier, we can use an intermediate temporary file to get this result inside the content.txt file:
$ awk '{print}' header.txt content.txt > content.txt.tmp
$ mv content.txt.tmp content.txt; cat content.txt
header: line-1
header: line-2
content: line-1
content: line-2
Perfect! We’ve got this one right.
5. Using cat and tee
Let’s explore another interesting approach using a combination of cat and tee commands to solve our use case. For this purpose, we’ll use the cat command within a command group and pipe its output to the tee command:
$ { cat header.txt; cat content.txt; } | tee content.txt.tmp
header: line-1
header: line-2
content: line-1
content: line-2
We must note that we’re using tee to duplicate the output to stdout and a temporary file named content.txt.tmp.
Lastly, let’s replace the content.txt file with the content.txt.tmp file:
$ mv content.txt.tmp content.txt; cat content.txt
header: line-1
header: line-2
content: line-1
content: line-2
The result looks as expected.
6. Using sed
sed one-liners are quite efficient for solving various text manipulation use cases. So, let’s see the power of the shortest possible sed one-liner command for concatenating multiple files:
$ sed '' header.txt content.txt
header: line-1
header: line-2
content: line-1
content: line-2
By default, sed prints the input files. So, we see the concatenated output of header.txt and content.txt files.
However, our goal is to have this output in the content.txt file. So, let’s apply our approach of output redirection to a temporary file, followed by renaming it to get the desired result:
$ sed '' header.txt content.txt > content.txt.tmp
$ mv content.txt.tmp content.txt
$ cat content.txt
header: line-1
header: line-2
content: line-1
content: line-2
The output looks fine.
With this, we’ve developed a good grip on solving the use case of appending text at beginning of a text file.
7. Conclusion
In this article, we learned how to append text at the beginning of a file. Further, we solved the use case using different utilities, such as cat, awk, tee, and sed.