Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Linux Commands»Mastering Multi-Line Editing: How to Write or Append Multiple Lines to a File in Linux

    Mastering Multi-Line Editing: How to Write or Append Multiple Lines to a File in Linux

    By RahulApril 10, 20232 Mins Read

    Managing and editing files is a crucial skill for any Linux user, whether you’re a system administrator, developer, or a general user. In this article, we will discuss various methods to write or append multiple lines to a file in Linux. We will cover the use of several command-line tools, including echo, printf, cat, tee, and text editors like nano, vim, and emacs.

    Contents

    1. Using echo and printf commands
    2. Using cat and tee commands
    3. Editing files with nano
    4. Editing files with vim
    5. Editing files with emacs
    6. Using Here Documents

    1. Using echo and printf commands

    The echo and printf commands are simple ways to write or append text to a file.

    • To write multiple lines to a new file, use the following syntax:
      echo -e "Line 1\nLine 2\nLine 3" > file.txt 
      
    • To append multiple lines to an existing file, use the double ‘>>’ operator:
      echo -e "Line 4\nLine 5\nLine 6" >> file.txt 
      
    • Alternatively, you can use printf:
      printf "Line 1\nLine 2\nLine 3" > file.txt 
      
      printf "Line 4\nLine 5\nLine 6" >> file.txt 
      

    2. Using cat and tee commands

    The cat and tee commands can be used to write or append multiple lines to a file.

    • To write multiple lines to a new file using cat:
      cat > file.txt << EOL
      Line 1
      Line 2
      Line 3
      EOL 
      
    • To append multiple lines to an existing file using cat:
      cat >> file.txt << EOL
      Line 4
      Line 5
      Line 6
      EOL 
      
    • Alternatively, you can use tee to write or append multiple lines:
      echo -e "Line 1\nLine 2\nLine 3" | tee file.txt 
      
      echo -e "Line 4\nLine 5\nLine 6" | tee -a file.txt 
      

    3. Editing files with nano

    Nano is a user-friendly text editor that comes pre-installed in most Linux distributions.

    To write or append multiple lines to a file using nano:

    nano file.txt 
    

    Type or paste the lines you want to add, then press 'Ctrl' + 'X' to exit. Press 'Y' to save the changes, and then press 'Enter' to confirm the filename.

    4. Editing files with vim

    Vim is a powerful text editor that is available in most Linux distributions.

    To write or append multiple lines to a file using vim:

    vim file.txt 
    

    Press 'i' to enter insert mode, type or paste the lines you want to add, and then press 'Esc' to exit insert mode. To save the changes and exit, type ':wq' and press 'Enter'.

    5. Editing files with emacs

    Emacs is another powerful text editor available on Linux.

    To write or append multiple lines to a file using emacs:

    emacs file.txt 
    

    Type or paste the lines you want to add, then press 'Ctrl' + 'X', followed by 'Ctrl' + 'S' to save the changes. Press 'Ctrl' + 'X', followed by 'Ctrl' + 'C' to exit emacs.

    6. Using Here Documents

    Here Documents is a shell feature that allows you to write multi-line text blocks within a script or command. They are particularly useful when working with large amounts of text or when you need to maintain a specific formatting.

    To write multiple lines to a new file using a Here Document:

    cat > file.txt << 'EOL'
    Line 1
    Line 2
    Line 3
    EOL 
    

    To append multiple lines to an existing file using a Here Document:

    cat >> file.txt << 'EOL'
    Line 4
    Line 5
    Line 6
    EOL 
    

    In both examples, 'EOL' is the delimiter that marks the beginning and end of the text block. You can replace 'EOL' with any string or keyword that is not present in the text block.

    Conclusion

    Mastering multi-line editing is an essential skill for any Linux user. In this article, we have discussed several methods to write or append multiple lines to a file in Linux, using command-line tools such as echo, printf, cat, tee, and text editors like nano, vim, and emacs. We also covered the use of Here Documents for managing large blocks of text. By understanding these techniques, you'll be better equipped to manipulate files and automate tasks on your Linux system.

    echo file
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Python Program to Copy a File

    How to Ignore SSL Certificate Check with Wget

    How to Ignore SSL Certificate Check with Curl

    View 12 Comments

    12 Comments

    1. nsynet on September 9, 2023 1:27 am

      my script as below:

      #! /bin/bash
      while [ 1 ]
      do
      sleep 1s
      str=”\””$(date)”\””
      echo ${str} >>333.csv

      # begin add next row
      echo “,\”” >>333.csv
      # append multiline result
      ps >>333.csv
      # end add next row
      echo “\”” >>333.csv

      # begin add next row
      echo “,\”” >>333.csv
      # append multiline result
      df -h >>333.csv
      # end add next row
      echo “\”” >>333.csv

      sed -i “:a;N;s/\”\n\,/\”\,/g;ta” ./333.csv
      done

      Reply
    2. 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
    3. 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
    4. 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
    5. Nishant Jaiswal on June 26, 2020 8:41 pm

      Method 3 is working for me, Thanks a lot

      Reply
    6. 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
    7. 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
    8. 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
    9. Jawad Kakar on September 13, 2019 1:31 pm

      Option 3 did the job!!! Thanks!

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

      very helpful, thx!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PIP on macOS: A Comprehensive Guide
    • Find Objects Between Two Dates in MongoDB: A Practical Guide
    • How to Check Packages Update History in Ubuntu
    • How to Grep for Contents after a Matching Pattern
    • How to Change Port in Next.Js
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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