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.

Advertisement

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 
    
  • To append multiple lines to an existing file using cat:
    cat >> file.txt 
    
  • 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 

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

cat >> file.txt 

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.

Share.

12 Comments

  1. 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

  2. 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

  3. 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

  4. 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

  5. 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 `(‘

    • 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.

  6. sanjeevi kumran on

    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.

    • 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

Leave A Reply


Exit mobile version