Luckily, there are several Linux commands you might use to do this.

Some of these commands are even powerful enough to insert text anywhere in your file.

Create a Text File First!

A laptop running Ubuntu with a Linux Terminal open

Hannah Stryker / How-To Geek

For example, we’re going to name it append.txt.

Now that we have an empty file, we can add some lines to it.

Do the same for “filename.”

Linux terminal display the touch command to create an empty text file

Let’s see that in action.

launch the below command:

So how to overcome this?

Instead, it’s possible for you to append the contents of one file to another.

Linux terminal shows how the echo command is used to add text to a file

you gotta combine them into a single file.

To do that, run:

3.

But did you know thatprintfis a Linux tool as well?

Linux terminal showcasing how the echo command can override the content of a text file

As the name suggests, it lets you print text or data to the standard output.

This means you should probably do it manually.

it’s possible for you to use the “\n” escape character at the end of your text.

The Linux terminal shows the use of the echo command to append a line to a text file

So let’s use that now.

This time, the terminal prompt is at a new line, unlike in the previous case.

Using sed

Thesed command, short for stream editor, performs basic text transformation on inputs.

Linux terminal showing the use of the cat command to combine the contents of different files

There are multiple ways to use this command.

The “$” sign means the line will be added at the end of the file.

Either use the “-a” option or the double redirection operator.

The Linux terminal shows how to use the cat command to combine the contents of several files and append them to another file

Let’s take a look at the first method.

After executing the command, you will enter interactive mode.

you’re able to write any text and press Enter to append it to the file.

The Linux terminal displaying the use of the printf command to append text to a file

Upon doing so, you will see the same text as output on the terminal.

To exit the prompt, press Ctrl+D.

Other than that, it works in the same way.

The Linux terminal shows how to use a new line character with the printf command

Using awk

Theawk commandis extremely powerful when it comes to text manipulation.

With some tweaking, you could add text to files using this command.

This one is rather straightforward.

The Linux terminal showing the use of sed command to append a line at the end of a file

For this tutorial, we’ll use thenano text editor.

If you better create a new line, do that by pressing Enter.

Then enter any text you want.

The Linux terminal demonstrating the sed command used to append a text line at a specific position of a file

Finally, save the file using Ctrl+O and exit the editor using Ctrl+X.

Suppose you run thels commandto get the contents of the present directory.

You want to save the list to a file called command.txt.

The Linux terminal showing the use of the tee command to append a line to a file

For that, launch the below command:

The same goes for any otherLinux command.

1 and 2 are file descriptors for standard output and standard error, respectively.

The Linux terminal showcasing the use of the tee command to append a line to a file

The Linux terminal displaying how to use the awk command to append a line to a file

The content of a file called append.txt that was opened in the Nano text editor program

The Linux terminal demonstrating how to save the output of a command to a file using the redirection operators