• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Write/Append Multiple Lines to a File on Linux

Written by Rahul, Updated on February 4, 2020

Sometimes you may be required to write or append multiple lines to a file. You can use multiple methods to write multiple lines to a file through the command line in the Linux system. Here are the three methods described below.

Method 1:-

You can write/append content line by line using the multiple echo commands. This the simple and straight forward solution to do this. We recommend going with Method 2 or Method 3.

echo "line 1 content" >> myfile.txt
echo "line 2 content" >> myfile.txt
echo "line 3 content" >> myfile.txt

Method 2:-

You can append content with the multi-line command in the quoted text. In this way can write multiple lines to fine with single echo command. But the third method is our suggested method to do this.

echo "line 1 content
line 2 content
line 3 content" >> myfile.txt

Method 3:-

This is the third and suggested option to use here documents (<<) to write a block of multiple lines text in a single command. The above methods are also useful but personally, I also use this while working.

cat >> greetings.txt <<EOL
line 1 content
line 2 content
line 3 content
EOL

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

8 Comments

  1. Avatar Nishant Jaiswal Reply
    June 26, 2020 at 8:41 pm

    Method 3 is working for me, Thanks a lot

  2. Avatar Anna Reply
    May 16, 2020 at 7:39 pm

    Hello , i want to append multiple lines to sshd_config.
    like below :
    Match Address
    PermitRootLogin without-password

    It should exactly in above format ..Could you please me using sed command

  3. Avatar jayesh Reply
    February 3, 2020 at 3:46 pm

    i apand to this :- echo -n GENERICS_DOMAIN_FILE(`/etc/mail/sendmail.gdf’) jayesh.txt
    below error please any solution
    -bash: syntax error near unexpected token `(‘

    • Rahul Rahul Reply
      February 4, 2020 at 5:52 am

      Hi Jayesh, Use the command as following:

      echo -n 'GENERICS_DOMAIN_FILE(`/etc/mail/sendmail.gdf’)' >> jayesh.txt
      

      Using the -n will not add the trailing newline.

  4. Avatar sanjeevi kumran Reply
    October 15, 2019 at 7:01 am

    I want to add a word in a specific line in the file. How can I do that? Do I need to mention the line number? Thanks in advance.

    • Rahul Rahul Reply
      October 16, 2019 at 4:59 am

      You can use the sed command to do this: Below example will append a string to line 4 in file.txt.

      sed -i ‘4s/$/ morestring/’ file.txt

  5. Avatar Jawad Kakar Reply
    September 13, 2019 at 1:31 pm

    Option 3 did the job!!! Thanks!

  6. Avatar GDSmith Reply
    September 5, 2019 at 3:57 pm

    very helpful, thx!

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Debian 10
  • Download Ubuntu 20.04 LTS – DVD ISO Images
  • Linux Run Commands As Another User
  • How to Check PHP Version (Apache/Nginx/CLI)
  • How To Install and Configure GitLab on Ubuntu 20.04
  • How to Install PyCharm on Ubuntu 20.04
  • How to Check Ubuntu Version with Command or Script
  • How to Set all directories to 755 And all files to 644
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy