Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

How to Append to End of a File in Linux

by Editorial Staff
June 13, 2020
in Linux
Reading Time: 4 mins read

In this tutorial, we learn different ways to append text to the end of a file in Linux. You can achieve this using several methods in Linux, but the simplest one is to redirect the command output to the desired filename. Using the >> character you can output result of any command to a text file.

Other ways this can be achieved, is using Linux tools like tee, awk, and sed.

Table of Contents
Redirect output of command or data to end of file
Adding lines to end of file
Adding command data output result to end of file
Alternative methods
Using tee command line tool
Using awk command line tool
Using sed command line tool
Append multiple lines to a file
Conclusion

Redirect output of command or data to end of file

Every Unix-based operating system has a concept of “a default place for output to go”. Everyone calls it “standard output”, or “stdout”, pronounced standard out. Your shell (probably bash or zsh) is constantly watching that default output place. When your shell sees new output there, it prints it out on the screen so that you can see it.

We can redirect that output to a file using the >> operator.

The procedure is as follows:

Append text to end of file using echo command:

echo 'sample text line' >> filename.txt

Append command output to end of file:

command >> filename.txt

Adding lines to end of file

We can add text lines using this redirect character >> or we can write data and command output to a text file. Using this method the file will be created if it doesn’t exist.

For example:

$ echo "sample line" >> test.txt
$ cat test.txt 
sample line

$ echo "sample line 2" >> test.txt
$ cat test.txt 
sample line
sample line 2

Adding command data output result to end of file

You can also add data or run command and append output to the desired file. In this example, we will use date for appending the current date to a file, uname command – which will print out kernel version of the Linux system we are using and lastly ls command which outputs the current directory structure and file list.
You can use any command that can output it’s result to a terminal, which means almost all of the command line tools in Linux.

$ date >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019


$ uname -r >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019
3.13.0-170-generic


$ ls >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019
3.13.0-170-generic
test.txt

Alternative methods

Lets see how to append using tee, awk and sed Linux utility.

Using tee command line tool

Tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It breaks the output of a program so that it can be both displayed and saved in a file.

$ tee -a test.txt <<< "appended line of text"
[email protected]:~$ cat test.txt 
appended line of text

Using awk command line tool

Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing.

$ awk 'BEGIN{ printf "another text line appended" >> "test.txt"  }'
[email protected]:~$ cat test.txt 
another text line appended

Using sed command line tool

Sed command in Linux stands for stream editor and it can perform lots of functions on a file like searching, find and replace, insertion or deletion. By using sed you can edit files even without opening it, which is a much quicker way to find and replace something in the file.

$ sed -i '$a yet another text line' test.txt
[email protected]:~$ cat test.txt 
yet another text line

Append multiple lines to a file

There are several ways to append multiple lines to a file at once.
You can, of course, add lines one by one:

$ echo "line 1" >> result.txt
$ echo "line 2" >> result.txt

Next variant is to enter new line in terminal:

echo "line 1
line 2
line 3" >> result.txt

Another way is to open a file and write lines until you type EOT:

$ cat <<EOT >> result.txt
line 1
line 2
EOT

Conclusion

Although there are multiple ways to append the text lines or data and command output to a file, we saw that the easiest way is using >> redirect character. There are ways to append the text to an end of a specific line number in a file, or in the middle of a line using regex, but we will going to cover that in some other article.

Let us know what method to append to the end of the file you think is best down in the comments section.

 

Related Posts

Apache, MySQL Performance

How to Optimize Apache, MySQL Performance for 1GB RAM VPS Centos/RHEL

April 7, 2024
Top 5 MySQL Performance Tuning Tips

Top 5 MySQL Performance Tuning Tips

January 25, 2023

Top Commands to Check the Running Processes in Linux

May 27, 2022

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

How to Transfer Windows 10 to SSD?

April 14, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Articles

  • Veo Camera Alternatives Recording and Analysing Sports Matches
  • Best 10 WizTree Alternatives for Disk Space Analyzer
  • Liner AI: The AI Search Engine and Alternatives
  • Webcam Toy: Complete Guide to Features & Alternatives
  • Google Vids Competitors For Online Video Creator Editor
  • 8 Best Free GameCube Emulators in 2025
  • SpyDialer Reverse Phone Lookup and Competitors

Related Posts

None found

  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.