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 echo
, printf
, cat
, tee
, and file redirection operations. Let’s explore each of these methods.

Table of Contents
How to Append To a File in Linux Bash – 5 Examples
Example #1: Using the echo command in Linux
The 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: Using the cat command
The 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: Use the bash tee Command
The 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: Use the printf command in Linux
Similar to echo
, the 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
:
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: Use Linux 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.

How to Append a Line to a File in Bash Linux?
Appending a blank line to a file in Bash can be accomplished with the ‘echo’ or ‘printf’ command followed by the ‘>>’ operator. Here are a few examples:
echo "" >> filename.txt
– This command appends a blank line to the end of filename.txt.echo -e "\n" >> filename.txt
– The ‘-e’ flag enables interpretation of backslash escapes and ‘\n’ represents a newline character, thus adding a blank line.printf "\n" >> filename.txt
– Similar to the second example, this command uses ‘printf’ to append a newline, thereby adding a blank line to filename.txt.
How to Append String Text to End of File in Bash Linux?
Appending text to the end of a file without altering the existing content is a common task when working with files. This operation can be accomplished using the ‘>>’ operator in a command-line interface.
Consider a situation where you have a file named “example.txt” and you want to add the phrase “Additional Text” to the end of it. In Linux or MacOS, you would open your terminal and input the following command: echo "Additional Text" >> example.txt
.
After executing these commands, “Additional Text” will be added to the end of “example.txt”, preserving the original content. This method provides a quick and easy way to append information to files directly from the command line.
How to Append String Text To Beginning of File in Bash Linux?
Adding text to the beginning of a file in Linux requires a slightly different approach compared to appending at the end. The ‘sed’ command-line utility can be used for this purpose.
Consider a situation where you want to prepend the text “Initial Text” to a file named “example.txt”. You can use the ‘sed’ command as follows: sed -i '1s/^/Initial Text\n/' example.txt
.
In this command, -i
enables in-place editing of the file. The ‘1s/^/Initial Text\n/’ part instructs ‘sed‘ to substitute the start of the first line (^) with “Initial Text” followed by a newline character (\n).
After execution, “Initial Text” will be added to the beginning of “example.txt”, preserving the original content. This use of ‘sed’ provides a powerful method to prepend information to files directly from the command line.
How to Concatenate Strings Using Bash?
In Bash scripting, string concatenation can be achieved by simply placing the strings or variables next to each other. Here’s how:
- Direct concatenation:
str="Hello" + " World!"
will produce the string “Hello World!”. - Using variables:
str1="Hello, "
str2="World!"
str3=$str1$str2
echo $str3
This script would output “Hello, World!”.
- Another method involves using curly braces
{}
to clearly delineate the variable names:
str1="Hello, "
str2="World!"
str3="${str1}${str2}"
echo $str3
This also outputs “Hello, World!”. Remember, Bash doesn’t require a specific concatenation operator.
Is There An Append Command in Linux?
When working with files in a Linux environment, you might find yourself needing to append data to an existing file. While there’s no specific “append” command, Linux offers a number of powerful tools to accomplish this. See the examples above!
was helpful
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 !!
Awesome, thanks!
Thanks a lot.. really helped.:)
this was really helpful…
Hi,
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
“abc
def
ghi
jkl
mno”
Now I need to create a new file which should contain the contents of both the file like below
“xyz abc
xyz def
xyz ghi
xyz jkl
xyz mno”
Is there any way in Unix to achieve the same ?
Any type of help will be much appreciated.
Thanks,
Prashant
Was very helpful.
Thanks a lot..
Respected Sir
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`
> do
> echo “`cat sample1.txt` $x” >> sample3.txt
> done
user@host0 ~]$ cat sample3.txt
xyz abc
xyz def
xyz ghi
xyz jkl
xyz mno
Thanx man. Really helpful tip.
Really halpful . thanks lot..
Hasan,
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;
Hi everyone,
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
sample.xml
to
appended sample.xml with “valid” as reference string
Regards