Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»How to Write/Append Multiple Lines to a File on Linux

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

    RahulBy RahulSeptember 14, 20171 Min ReadUpdated: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
    
    echo file
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleGet A Bit Closer To Java 8
    Next Article Notepadqq – An Alternative of Notepad++ for Linux

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    11 Comments

    1. Venkatesh on March 28, 2021 8:38 am

      HI Sir ,
      I need to add multiple lines of text(22 lines ) at the beginning of a file .
      I have tried the method 3 above but apending happening at the End of File .Please help me with the command.In anticipation of your reply

      Thanks

      Reply
    2. Venkatesh on March 28, 2021 8:35 am

      I have tried the method 3 but it is append at the end of the file

      Reply
    3. Venkatesh on March 28, 2021 8:34 am

      Hi sir,
      I need to add multiple lines of text(22 lines ) at the beginning of file.

      PLease help me with the command .In anticipation of your reply

      Reply
    4. Nishant Jaiswal on June 26, 2020 8:41 pm

      Method 3 is working for me, Thanks a lot

      Reply
    5. Anna on May 16, 2020 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

      Reply
    6. jayesh on February 3, 2020 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 `(‘

      Reply
      • Rahul on February 4, 2020 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.

        Reply
    7. sanjeevi kumran on October 15, 2019 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.

      Reply
      • Rahul on October 16, 2019 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

        Reply
    8. Jawad Kakar on September 13, 2019 1:31 pm

      Option 3 did the job!!! Thanks!

      Reply
    9. GDSmith on September 5, 2019 3:57 pm

      very helpful, thx!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    • How to Create DMARC Record For Your Domain
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.