Append to a file through a Linux terminal without a text editor

When manipulating files through the terminal, we sometimes require adding a line of text to a file. We can install and use different text editors, such as “Vim” or “Nano,” but we will have to open the file using the editor, scroll down, and only then be able to add the line manually.

However, we can still append to a file through the terminal if we don’t want to install these editors. We can either append a single line to a file or append all the contents of one file to another.

Append a single line to a file

We can append any line to a file using the following syntax:

echo "Line" >> Filename
  • Line: This will be the line of text we are adding to the file.

  • Filename: It will be the name of the file to which we want to append. If that file doesn't exist, an empty file with this name will be created, and the line will be added.

Be careful to use double greater-than sign (>>), and not a single. A single greater-than sign (>) will result in the file being rewritten, containing only the line of text we are appending.

We can practice in the following terminal:

Terminal 1
Terminal
Loading...

In the terminal above, we add a line of text to the file that wasn't there before, thus creating a new file. Then, we append a new line of text to this newly created file.

Append one file to another

The syntax for appending a file to another is quite similar to the one we used before. It is as follows:

cat AppendFilename >> Filename
  • AppendFilename: This will be the file’s name, whose contents will be appended to another file. If this file doesn’t exist, the command will throw an error.

  • Filename: It will be the name of the file to which we want to append. If this file doesn’t exist, an empty file with this name will be created, and the contents of the first file will be appended to it.

  • The extensions of both files don’t need to be the same.

Use double greater-than signs (>>), not single because a single greater-than sign (>) will result in the Filename being rewritten, containing only the contents of AppendFilename.

Run the following terminal and see the output:

Terminal 1
Terminal
Loading...

In the terminal, we first create a new file main with 2 lines of text. Then, we create a new file, append.txt, containing 2 lines of text. We append the contents of append.txt to main, after which the latter contains 4 lines of text, including the contents of the former.

Conclusion

One thing to notice is that if we were to run echo "Line" command only, the Line would have been displayed on the terminal; if we were to run cat AppendFilename command only, the contents of AppendFilename would have been displayed on the terminal. The double greater-than symbols (>>) only redirect the output to the specified file. This shows that anything displayed to the terminal can be appended to a file using >>.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved