- Append Lines to a File in Linux
- Using echo command
- Using cat command
- Using tee command
- Using printf command
- Using sed command
- Using awk command
- Using file redirection operator
- Using paste command
- Using ed command
- Conclusion
- How to append multiple lines to a file
- 10 Answers 10
- How to append output to the end of a text file
- > for Overwrite
- >> for Append
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 > filename.txt New line of data EOF
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 −
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 −
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 −
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.txtThe "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.
How to append multiple lines to a file
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
Host localhost ForwardAgent yes
10 Answers 10
# possibility 1: echo "line 1" >> greetings.txt echo "line 2" >> greetings.txt # possibility 2: echo "line 1 line 2" >> greetings.txt # possibility 3: cat > greetings.txt line 1 line 2 EOT # possibility 4 (more about input than output): arr=( 'line 1' 'line 2' ); printf '%s\n' "$" >> greetings.txt
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1: echo "line 1" | sudo tee -a greetings.txt > /dev/null # possibility 3: sudo tee -a greetings.txt > /dev/null
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough: < echo "line 1" ; echo "line 2"; >>>greetings.txt
echo -e "Hello \nWorld \n" >> greetings.txt
printf '%s\n %s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%s\n\t%s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo , but exactly how varies from implementation to implementation, whereas printf is consistent.
Another approach is to use tee
A few choice lines from tee 's man page:
The tee utility copies standard input to standard output, making a copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
+1 tee tends to work with paths that require sudo (solutions that use > >> <
Here is an example to append multiple lines in a file:
< echo ' directory "/var/cache/bind";' echo ' listen-on < 127.0.0.1; >;' echo ' listen-on-v6 < none; >;' echo ' version "";' echo ' auth-nxdomain no;' echo ' forward only;' echo ' forwarders < 8.8.8.8; 8.8.4.4; >;' echo ' dnssec-enable no;' echo ' dnssec-validation no;' > >> your_file.txt
It is worth to note that this variant is part of the ShellCheck recommendation github.com/koalaman/shellcheck/wiki/SC2129
SED can append a line to the end of a file like so:
sed -i '$ a text to be inserted' fileName.file
$ selects end of file, the a tells it to append, and after this comes the text that is to be inserted. Then of course the file name.Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this: find . -name "*.html" -exec sed -i '$ a