HeatWare.net header image
HeatWare.net
TwitterRSSEmail
  • Home
  • Windows
    • Windows 8
    • Windows 7
    • Windows Vista
    • Windows XP
  • Linux/Unix
  • Mac
  • Mobile
    • Android
    • iOS
    • Phones (Help / Resources)
  • Software / Programming
    • Free Software
    • Programming – General
    • PHP
    • Ruby/Rails
    • Quality Assurance (QA)
    • Software – General
    • Software Help
    • Databases
  • Cool Websites
  • Other
    • Deals & Bargains
    • News
    • Video Games
    • Hardware
    • Electronics
  • About

25 comments / February 23, 2009 / sood / Linux/Unix

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

  1. To append the string “hello” to file greetings.txt
    echo "hello" >> greetings.txt
  2. To append the contents of the file temp.txt to file data.txt
    cat temp.txt >> data.txt
  3. To append the current date/time timestamp to the file dates.txt
    date >> dates.txt

Take it one step further – Find out how to insert a string to the beginning of a file

Related Posts

  • Insert/Add String to Beginning of a File
  • Using Linux ‘head’ command to preview a file
  • Free Linux utility to convert PDF to Text file
  • Using 'tail' to output updates/appendings to a file in Linux
  • How to open/uncompress a .tar.bz2 file

Linux

25 comments… add one
  1. Tim November 18, 2010, 7:41 pm

    was helpful

  2. Marcinkonys October 5, 2011, 4:18 pm

    Thanks a lot!

  3. ST November 11, 2011, 3:50 am

    realy helpful, thanks!

  4. Alexey November 27, 2011, 8:51 am

    Thank you, it helped me a lot.

  5. das January 3, 2012, 1:37 am

    how can add the data to the file without open the file in UNIX/Linux ?

  6. fabio January 17, 2012, 6:45 am

    Great article!
    Thanks

  7. Manish January 28, 2012, 5:46 am

    Thanks, ’twas helpful !!

  8. jin February 8, 2012, 3:37 pm

    Awesome, thanks!

  9. Ashok April 17, 2012, 5:00 am

    Thanks a lot.. really helped.:)

  10. Sood (Admin) April 17, 2012, 9:00 am

    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?

    Thanks,
    Sood

  11. Eddie May 14, 2012, 1:29 pm

    this was really helpful…

  12. Bunty May 21, 2012, 2:14 am

    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

  13. Prashant Singh October 12, 2012, 8:19 pm

    Was very helpful.
    Thanks a lot..

  14. aditya menon January 6, 2013, 1:16 am

    I thank you for your brevity.

  15. karthik February 17, 2013, 6:08 am

    Respected Sir

    How to edit the contents in unix through command

  16. karthik February 17, 2013, 6:09 am

    Sir, Your giving append example really helpfull for me. Thax alot

  17. Aline February 22, 2014, 5:04 am

    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!

  18. Phạm Phước Lộc July 22, 2014, 12:09 am

    Thank you so much! I have been looking for this all the morning!

  19. Ernst March 4, 2015, 12:51 am

    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

  20. Vagelis Prokopiou March 25, 2015, 12:03 pm

    Thanx man. Really helpful tip.

  21. shan June 12, 2015, 4:53 am

    Really halpful . thanks lot..

  22. JAMTANGAN FORTUNER June 13, 2015, 4:19 am

    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.
    Cheers!

  23. Hasan Rumman February 12, 2016, 8:54 am

    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

  24. sood February 23, 2016, 6:40 pm

    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;

  25. Tarun December 15, 2016, 9:51 am

    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

Cancel reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Why You Should Consider Diving Apps
  • How to Buy a Smartphone that Fits Your Budget
  • How to Overcome Frustrating PDF Stress
  • Convert PDF to Word: Easy, Reliable and Quality Conversion
  • PDF to Excel App ­ – A New Way of Handling Tricky Documents

Recent Comments

  • Valarie Walter on Basic Troubleshooting Steps for your Cell Phone
  • John Mists on A Brief History of Android OS
  • syarif on PostgreSQL: How to reload config settings without restarting database
  • Raghu on How to SSH to a server using Ruby – Part I
  • francisco clemente on Basic Troubleshooting Steps for your Cell Phone

Tags

ACSLS Android Bargain Cell Phones Cool Software Cool Websites Databases Deals Ebooks Facebook Free Software G2x Galaxy S5 iOS iPhone Kindle LG Linux Linux/Unix Mac Mobile mysql Office OpenStack OS X PHP Postgres PostgreSQL ruby Samsung Galaxy S6 Shell Smartphones Sun T-Mobile Tips Tips & Tricks Ubuntu Unix Virtualization VMWare Windows Windows 7 Windows 8 Windows Vista Windows XP

Latest Tweet

Follow @HeatwaredotNet

SP
@HeatwaredotNet

  • Why You Should Consider Diving Apps https://t.co/Is41cdUv2I #diving-apps
    about 6 years ago

All Categories

Other Links

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2015 — HeatWare