This article will show you how to append to a file in Bash Linux, such as a string. All of the following examples should work on all major Linux distributions like Ubuntu, CentOS, RHEL, Arch, and Fedora.
One of the most essential skills for users and system administrators alike is the ability to manage and manipulate files effectively. Among the various file operations, appending data to a file is a common requirement. This article provides a comprehensive understanding of how you can append to a file in Linux with real-life examples.
Appending refers to adding or concatenating additional information at the end of an existing file. Linux provides several ways to achieve this, utilizing command-line utilities such as
tee, and file redirection operations. Let’s explore each of these methods.
Table of Contents
Linux Append To a File: 5 Ways
Example #1: echo Command
echo command is often the go-to method for appending simple text to a file. For example, if you want to add the text “Hello, World!” to the end of a file named ‘example.txt’, you would use:
echo "Hello, World!" >> example.txt
The ‘>>’ operator redirects the output of the
echo command, appending it to the end of ‘example.txt’. If the file does not exist, it is created.
Example #2: cat Command
cat command, short for ‘concatenate’, is used primarily to display or combine files. However, you can also use it to append the content of one file to another. To append the content of ‘file1.txt’ to ‘file2.txt’, you would use:
cat file1.txt >> file2.txt
If you want to append some lines of text into a file interactively, you can use
cat with the append operator (‘>>’) and a file name:
cat >> example.txt After running this command, you can enter the text you want to append and press Ctrl+D to exit.
Example #3: tee Command
tee command reads from standard input and writes to standard output and files. If used with the ‘-a’ or ‘–append’ option,
tee will append the input to the specified files. Here is an example:
echo "Hello, World!" | tee -a example.txt
Unlike the previous methods,
tee displays the input on the screen and also appends it to the file.
Example #4: printf Command
printf command can also append text to a file. The
printf command provides more formatting options than
echo. Here’s how you would append the same text using
printf "Hello, World!\n" >> example.txt
Notice the ‘\n’ at the end of the string.
printf doesn’t automatically add a newline character, so you’ll need to include it manually if desired.
Example #5: File Redirection
In Linux, you can also use file redirection to append the output of a command to a file. For instance, the command
ls -l >> file.txt will append the output of the
ls -l command (which lists the files in the current directory in long format) to ‘file.txt’.:
In conclusion, Linux offers a variety of ways to append data to files, each with its unique features and use-cases. Mastering these commands will empower you to manage and manipulate your files efficiently. Remember, it’s not just about knowing the commands, but also understanding when to use each one effectively. The power of Linux truly shines when you leverage its tools to meet your specific needs.
Take it one step further – Find out how to insert a string to the beginning of a file
20 thoughts on “Linux 101: Append String/Data To A File”
Thanks a lot!
realy helpful, thanks!
Thank you, it helped me a lot.
how can add the data to the file without open the file in UNIX/Linux ?
Thanks, ’twas helpful !!
Thanks a lot.. really helped.:)
this was really helpful…
I am having two text file (Let’s assume sample1.txt & sample2.txt). sample1.txt contains only one line “xyz” while sample2.txt contains many lines like
Now I need to create a new file which should contain the contents of both the file like below
Is there any way in Unix to achieve the same ?
Any type of help will be much appreciated.
Was very helpful.
Thanks a lot..
How to edit the contents in unix through command
Sir, Your giving append example really helpfull for me. Thax alot
Thank you very much! I was looking for how to set a new line in a txt file, and just add one echo per line in the loop resolve it. Thank you!
Thank you so much! I have been looking for this all the morning!
In response to Prashant:
user@host0 ~]$ for x in `cat sample2.txt`
> echo “`cat sample1.txt` $x” >> sample3.txt
user@host0 ~]$ cat sample3.txt
Thanx man. Really helpful tip.
Really halpful . thanks lot..
Use this to find all the files called test.txt under the current tree and to append a line:
for f in `find . -name test.txt`; do echo “test” >> $f; done;
I am looking to append a string to a particular string as a reference in sample.txt or sample.xml, Can Someone shed some light on it
appended sample.xml with “valid” as reference string