How to append string/data to a file in Linux
This article will show you how to append a string (or any data) to the end of a file under Linux/Unix. Appending is done very simply by using the append redirect operator >>. Simply use the operator in the format data_to_append >> filename and you’re done. Below are several examples:
Examples to Append Data to a File
- To append the string “hello” to file greetings.txt
echo "hello" >> greetings.txt
- To append the contents of the file temp.txt to file data.txt
cat temp.txt >> data.txt
- To append the current date/time timestamp to the file dates.txt
date >> dates.txt
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.:)
Thanks for the comments. Are there any other examples that you think should be added to this article? Anything else you all are looking for?
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..
I thank you for your brevity.
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..
Hello! Quick question that’s totally off topic. Do you know
how to make your site mobile friendly? My blog looks weird when browsing from my iphone4.
I’m trying to find a theme or plugin that might
be able to resolve this issue. If you have any recommendations, please share.
Thanks a ton. It was really helpful.
I got one question: If i have same file test.txt in multiple directories, like dir1, dir2, dir3 how can I add a string using single command.
i have tried below command (i know i’m stupid)
echo “test” >> dir*/test.txt
and got below error:
-bash: /home/blnc/file*/test.txt: ambiguous redirect
Your help is greatly appreciated in advance
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