how to append or add text to a file in linux

Append is defined as “to add something new to something that is existing, as an attachment or supplement“. Sometimes you will need to append text to an already existing text file. This text could come from any source, such as a line of text from the command line, the output of a command or from another text file.

The easiest way to append text is to use the operator ‘>>‘ from the Linux command line. There are two different operators that redirects output to files: ‘>‘ and ‘>>‘, that you need to be aware of.

>‘ is used to remove the previous contents before appending the text, while ‘>>‘ appends text while preserving the existing contents. And also, it always appends the text to the end of the file. So, if you want to append text to the end of the text then you should use the “>>” operator in your commands. We will see some examples of how this is done.

Append Single Line of Text

If you just want to quickly append a small single line of text, then you can use the echo command from the command line.

bash$ echo "This is just a single line of text" >> ./path/filename.txt

Append Text from Command Prompt (Multiple Lines)

If you want to append a long line of text or multiple lines then using the echo command can be cumbersome. In this case, you can direct the stand input (stdin) to the file instead.

When you use the following command, the cat command reads from the stdin and the “>>” operator redirects the standard input to the file, which means you will not return back to the prompt unless you exit (close the stream) using Ctrl-D

bash$ cat >> ./path/filename.txt

Using cat and />> to append text to file” class=”wp-image-2248″/></figure></div>



<p>Note: use <strong>ctrl-d</strong> to exit the input mode</p>



<h3>Append Text from another File</h3>



<p>The text than you want to append can come from another text file. In this case, you can use the <em><strong>cat</strong></em> command along with the append operator to append the content.</p>



<p><code>bash$ cat myfile.txt >> ./path/filename.txt</code></p>



<p>You can also use the <a href=cat and append operators to merge multiple files as well. If you only want to append specific lines from the text file into the output file, then you may use the grep command to filter the output of cat and then append the results to file.

For example, the following command will append all lines that contain the word chocolate into the file chocolate.txt.

bash$ cat myfile.txt | grep -i "chocolate" >> ./path/chocolate.txt

Append Command Output to File

This works pretty much the same way as described with the earlier examples. The only requirement is that the command that you are using actually do output the results to the standard output.

bash$ date >> ./path/filename.txt

The above command appends the output of date command to the file name filename.txt. You can use any other command as well, such as time, ls, find or grep.

Using sed

Using the “>>” (append) operator is the easiest way to append text to a file from the command line. The other option that you have is the use of sed command.

The following example will append the line “Why redirection? I can use sed.” into the file filename.txt.

bash$ sed -i "$ a\Why redirection? I can use sed." ./path/filename.txt

The one advantage of sed is that you actually use it to prepend text, as opposed the append. Prepend will add the new text to to the start of the file, while append adds it to the bottom or end of the file.

To prepend text to a file you can use the option 1i, as shown in the example below.

bash$ sed -i '1i This is the start of the file' ./path/filename.txt

The sed command is a really powerful tool when it comes to the text manipulation. It is worthwhile reading through the documentation to find all its features.