Append Lines to a File in Linux


Introduction In Linux, files are often used to store data that is either created by a program or generated by user. It is common for users to append new lines of data to existing files rather than creating new ones from scratch. This article will explain how to append lines to a file in Linux, including several subheadings and examples.

Using echo command

The easiest way to append new lines to a file is by using echo command. echo command allows you to display a message on screen, but it can also redirect message to a file. Here's how to use echo command to append new lines to a file −

$ echo "New line of data" >> filename.txt

The ">>" symbol appends new line of data to end of file. If file does not exist, echo command will create a new file.

Using cat command

The cat command is another way to append new lines to a file. cat command is used to concatenate files, but it can also be used to append new lines to a file. Here's how to use cat command to append new lines to a file −

$ cat <<EOF >> filename.txt
New line of data EOF

The "<<" symbol tells cat command that following lines are input to command. "EOF" symbol indicates end of input.

Using tee command

The tee command is another way to append new lines to a file. tee command is used to display output on screen and also redirect it to a file. Here's how to use tee command to append new lines to a file −

$ echo "New line of data" | tee -a filename.txt

The "-a" option tells tee command to append new line of data to end of file. If file does not exist, tee command will create a new file.

Using printf command

The printf command is another way to append new lines to a file. printf command is used to format and print data, but it can also redirect output to a file. Here's how to use printf command to append new lines to a file −

$ printf "New line of data
" >> filename.txt

The "
" symbol tells printf command to create a new line.

Using sed command

The sed command is a powerful tool for manipulating text in Linux. sed command can also be used to append new lines to a file. Here's how to use sed command to append new lines to a file −

$ sed -i '$aNew line of data' filename.txt

The "-i" option tells sed command to edit file in place. "$" symbol tells sed command to append new line of data to end of file.

Using awk command

The awk command is another powerful tool for manipulating text in Linux. awk command can also be used to append new lines to a file. Here's how to use awk command to append new lines to a file −

$ awk 'BEGIN{print "New line of data"}' >> filename.txt

The "BEGIN" symbol tells awk command to execute following command before reading input. "print" command tells awk command to print new line of data.

In addition to methods discussed above, there are other ways to append lines to a file in Linux that are worth exploring. Here are some other options −

Using file redirection operator

The file redirection operator "> >" is used to append new lines to a file in Linux. Here's how to use it −

$ command >> file

Where "command" is command whose output is to be appended to "file". For example, to append output of "ls" command to a file called "file.txt", you would use following command −

$ ls >> file.txt

Using paste command

The paste command is used to merge lines of files. However, it can also be used to append new lines to a file. Here's how to use paste command to append new lines to a file −

$ paste -s -d'
' file.txt - >> new_file.txt

The "-s" option tells paste command to merge lines. "-d" option specifies delimiter to use when merging lines. In this case, delimiter is a newline character. "-" symbol tells paste command to read input from standard input. ">>" symbol appends output to end of file.

Using ed command

The ed command is a line editor that can be used to edit files in Linux. Here's how to use ed command to append new lines to a file −

$ echo "a" >> filename.txt 
$ echo "New line of data" >> filename.txt 
$ echo "." >> filename.txt 
$ ed filename.txt <<EOF 
$a 
New line of data 
. 
wq 
EOF

The "a" command tells ed command to enter append mode. "wq" command tells ed command to write changes to file and quit.

Conclusion

Appending new lines to a file in Linux is a common task for users and developers alike. There are several ways to append new lines to a file, including echo, cat, tee, printf, sed, and awk commands. Each command has its own advantages and disadvantages, so it is important to choose right command for job. By mastering techniques outlined in this article, you will be able to append new lines to files in Linux with ease, improving your productivity and efficiency.

Updated on: 14-Mar-2023

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements