Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at: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

    By RahulFebruary 4, 20201 Min Read

    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.

    Advertisement

    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

    Related Posts

    Split Command in Linux With Examples (Split Large Files)

    Test Your Internet Speed from the Linux Terminal

    11 Practical Example of cat Command in Linux

    View 11 Comments

    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

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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