The Quickest Way to Append Text at the Beginning of a File in Linux

As you know, there are redirection operators, which allow you to append text to a file. When you use the >> operator, the text is appended to the end of the file, while the > operator replaces the existing content.

There are no specific commands or operators for these tasks, but don’t worry. We’ve figured out how to add text to the beginning of a file in Linux, and we’ll show you how.

After that, adding text will be a breeze, but before you do that, it’s important to make a copy of the original file to ensure that your data is safe and can be easily restored in the event of a mishap.

How the issue looks

For the sake of this article, I have created a file called “linux_distros,” in which I already populate the ten Linux distributions. In that, we will learn how to append text at the start of the file.

So let me show you the content of linux_distros, and then you will get a better idea of it.

Ubuntu 22.04
Debian 11
RHEL 9
Ubuntu 20.04
Debian 11
Linux Mint
AlmaLinux
POP!_OS
CentOS Stream
Debian 10

Append text to the beginning of text using the sed command

The sed command is an extremely useful tool in Unix and Linux operating systems, as it can be used to add or append text to the beginning of a file. You just need to know the syntax, and the command will perform its work like a charm.

Using the sed command, append text to the beginning of the file

To show you the beauty of sed, let me take one basic and simple sed syntax that will add text to the beginning of a file without having to add it line by line by hand.

$ sed '1i [PASS-YOUR-DATA]' [FILE-NAME]

Although command syntax is very simple to understand, I’ll still explain what a command will do when you run it. “1i” is like a number line on which you want to manipulate the data, “[PASS-YOUR-DATA]” is a data that you want to append, and the last is “[FILE-NAME]” on which you want to apply the following operation.

Let’s use the syntax from above to add the text “List of Linux Distributions” to the previously created file. So simply run the following command, which will append the message.

$ sed '1i List of Linux Distribution' linux_distros 

After executing the previous command, this is what happens:

Add title to the top of the file
Add a title to the top of the file

Once you are sure of the result, you can save the data to the original file by invoking the -i options, which will make direct changes to the file without printing data on STDOUT.

$ sed -i '1i List of Linux Distribution' linux_distros 

Append the text at the beginning of any character

To add text at the beginning of any character or word, you can use the command below, which automatically adds your text data where your previous content lies.

$ sed -i '1/^/[PASS-YOUR-DATA]/' [FILE-NAME]
  • -i: make a direct change to the source file
  • "1/": address space where you want to make changes
  • "/^/": caret used to specify the start of the line
  • "/PASS-YOUR-DATA/": replace with the data that you want to be added to your file
  • FILE-NAME : provide your sample file

For the sake of better understanding, I have created a sample file for you in which I simply listed down the numbers from 1 to 9, as you can see in the below image.

Sample file to append text at beginning
Sample file to append text at the beginning

Now I want to add the number 0 before the number 1, so it should look like “01.” To make changes like that, you should know on which line your text is lying. For me, “1″ lies on the number “2.”

So let me run the command and wait for the output to appear on the screen.

$ sed '2s/^/0/' sample 

After executing the previous command, this is what happens:

After appending zero with one
After appending zero with one

But the problem with the above solution is that it added only 0 to the number 1, and the rest are still pending to be appended. Can we add 0 to every number?  With sed, it is possible. All you have to do is define the address range as shown in the code below:

$ sed '2,10s/^/0/' sample 

After executing the previous command, this is what happens:

After appending zero to all number
After appending zero to all number

When you’ve checked that the above command gives you the results you want, you can add the -i option to make the changes permanent.

$ sed -i '2,10s/^/0/' sample 

That’s all it takes to use the sed command to append text to the beginning or start of a file, and if you know any other ways to achieve the same, then please let us know in the comment section.

It’s going to take a short while until we meet again.

Leave a Reply