7 Ways to append text to a file on Linux

Configurare noua (How To)

Situatie

There are times when you have a text file with existing content, and you need to append some more text to it. Luckily, there are several Linux commands you can use to do this. Some of these commands are even powerful enough to insert text anywhere in your file.

Solutie

To follow along with the tutorial, you should have a test file ready where you can append lines and see the result. To create an empty file on Linux, run the below command:

touch <filename>

Replace <filename> with the name of the file. For example, we’re going to name it append.txt. We can create the file with this command:

touch append.txt

To see the content of the file, run:

cat append.txt

You won’t get any output as currently, there is no content in the file. Now that we have an empty file, we can add some lines to it.

1. Using echo to Append Text to a File

The echo command is mostly used for displaying text on the terminal, but you can also use it to add text to a file. Here’s the basic syntax:

echo [OPTION] [STRING]

To use the echo command for adding lines to a file, you’ll need some help from the redirection operator (>). To add text lines to a file using the echo command, run:

echo "string" > filename

Replace “string” with actual text inside the double quotes. Do the same for “filename.” For example, if you’d like to add the text “This is a file” to append.txt, run:

echo "This is a file" > append.txt

Now if you try to read the file content using the cat command, you should see the line appear in the output.

However, with a single redirection operator (>), you won’t be able to append lines to the file. If you try to add another line to the above file using the same command, it will override the existing content of the file. Let’s see that in action. Run the below command:

echo "Trying to append a line to the file" > append.txt

So how to overcome this? Simple. To append some text to a file without overriding its content, use another redirection operator (>>), like this:

echo "Trying to append a line to the file" >> append.txt

This time, the line will get appended to the file, as expected.

2. Using cat to Append Text to a File

The cat command, as we’ve seen previously, displays the content of a file on the terminal. The basic syntax of the command is:

cat [OPTION]... [FILE]...

Unlike the echo command, the cat command can’t directly use any strings as parameters. Instead, you can append the contents of one file to another. To do that, use the command like this:

cat file1 >> file2

Replace “file1” and “file2” with the actual file names. So, in our previous example, we could append the content of another file to the append.txt file using:

cat anotherfile.txt >> append.txt

Another good way to use the cat command is to concatenate the contents of multiple files into a single file. For that, specify the file names as arguments, like this:

cat file1 file2 file3 >> file4

Say, you have three different text files containing the information of three different employees. You need to combine them into a single file. To do that, run:

cat employee1.txt employee2.txt employee3.txt >> employees.txt
3. Using printf to Add Text at the End of a File

You may be familiar with printf if you have experience with the C programming language. But did you know that printf is a Linux tool as well? As the name suggests, it lets you print text or data to the standard output. Here’s the basic syntax:

printf FORMAT [ARGUMENT]...

To append text to a file using printf, run:

printf "Using printf to append text" >> append.txt

However, as you can see, unlike the echo command, there was no new line appended to the text. This means you need to do it manually. You can use the “\n” escape character at the end of your text. So let’s use that now.

printf "Using printf to append text with a new line\n" >> append.txt

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

4. Using sed

The sed command, short for stream editor, performs basic text transformation on inputs. There are multiple ways to use this command. The basic syntax to append text to files looks something like this:

sed [OPTION] [SCRIPT] [FILE...]

Let’s now see how you can append a line to a file. For that, follow the below command format:

sed -i '$ a\<text to append>' <file name>

Replace the text inside the angle brackets (<>) with the text string, and your file name, respectively. So if you want to append a line at the end of the file, run:

sed -i '$ a Do not feel sed' append.txt

The “-i” option tells the system that it will use the file in question to insert the text. The “$” sign means the line will be added at the end of the file. Adding the “a” in front of our text tells the system that we want the line to be appended to the existing content of the file.

One good thing about the sed command is that you can add text anywhere in the file. So, for example, if you want to add something after the fourth line, use the command like this:

sed -i '4 a Appended after the fourth line' append.txt

This time, the line was added exactly after the fourth line.

5. Using tee to Append Text to a File

The tee command lets you read from standard input and write the output to files. The basic syntax is as follows:

tee [OPTION]... [FILE]...

There are two ways you can append text to a file using the tee command. Either use the “-a” option or the double redirection operator. Let’s take a look at the first method.

tee -a append.txt

After executing the command, you will enter interactive mode. You can write any text and press Enter to append it to the file. Upon doing so, you will see the same text as output on the terminal. To exit the prompt, press Ctrl+D.

In the other method, the terminal won’t repeat the string you type in, making the process much cleaner. Other than that, it works in the same way. Here’s the command:

tee >> append.txt

As you can see, when we typed in a string, it wasn’t displayed in the terminal again.

6. Using awk

The awk command is extremely powerful when it comes to text manipulation. With some tweaking, you can add text to files using this command. Here’s the syntax for the command:

awk 'BEGIN{print "Feeling awkward"}' >> append.txt

In the above example, we used the awk command’s BEGIN rule to print some text and send it to our target file using the redirection operators.

7. Using a Text Editor to Append Text

This one is rather straightforward. If you don’t like using commands or find it difficult to memorize the syntax, then you can simply use any Linux text editor to append lines to a text file. For this tutorial, we’ll use the nano text editor.

To open a file in nano, use the command below:

nano <filename>

Remember to replace <filename> with the path to an actual file in the above command. Once it’s opened in the editor, you can navigate through the text using the arrow keys and type in anything you like using your keyboard.

To append a line, all we need to do is use the down arrow key to go to the bottom of the file. If you need to create a new line, do that by pressing Enter. Then enter any text you want. Finally, save the file using Ctrl+O and exit the editor using Ctrl+X.

How to Redirect the Output of a Command to a File

If you want to save the output of a command to a file, you can do that using the redirection operators (>>). Suppose you run the ls command to get the contents of the present directory. You want to save the list to a file called command.txt. For that, run the below command:

ls >> command.txt

The same goes for any other Linux command. You write that command, add the redirection operators, and then the file name where you want to save the output.

How to Append Standard Output and Standard Error to a File

Before wrapping up, we want to show you another cool file manipulation trick in Linux. If you want to redirect both standard output and standard error, run:

command >> file.txt 2>&1

Instead of the “command” field, you need to type in a specific command whose output you want to capture. 1 and 2 are file descriptors for standard output and standard error, respectively. We also use an & sign to indicate that what comes before and after the redirection operator are file descriptors and not file names.

Tip solutie

Permanent

Voteaza

(1 din 3 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?