Linux append text to end of file

I am new Linux user and sysadmin for CentOS enterprise Linux 7 running on couple of VMs and bare metal boxes. How to add lines to end of file? How do I append text to end of file using the cli on Linux? What command I need to type to send the output of the command to end of file?


You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.

How to redirect the output of the command or data to end of file

The procedure is as follows

  1. Append text to end of file using echo command:
    echo 'text here' >> filename
  2. Append command output to end of file:
    command-name >> filename

How to add lines to end of file in Linux

The >> is called as appending redirected output. Create the file if does not exists. For example, append some networking command to script:
echo 'I=eth0' >>
echo 'ip link set $I up' >>
echo 'ip addr add dev $I' >>
echo 'ip route add default via' >>

You can also add data to other config files. Another option is to run command and append output to a file. Run data command at the terminal and append output to output.txt:
date >> output.txt
Execute ls command and append data to files.txt:
ls >> files.txt
To see files.txt use cat command:
cat files.txt
more files.txt
less files.txt

How to append standard output and standard error

The following sytax allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file name. The format for appending standard output and standard error is:
echo 'text' &>>filename
command &>>filename
find . type d -name "*.projects" &>> list.txt

This is semantically equivalent to
echo 'text' >>fileNameHere 2>&1
command >>fileNameHere 2>&1
date >>data.txt 2>&1

How to add lines to end of file in Linux or Unix
For more info read redirection topic.

Append text when using sudo

Try the tee command:
echo 'text' | sudo tee -a my_file.txt
echo '' | sudo tee -a /etc/hosts

Of coruse we can use following syntax to append text to end of file in Linux
sudo sh -c 'echo my_text >> file1'
sudo -- bash -c 'echo "some data" >> /my/path/to/filename.txt'

The -c option passed to the bash/sh to run command using sudo.
See “how to append text to a file when using sudo command on Linux or Unix” for more info.

Conclusion – Append text to end of file on Unix

To append a new line to a text on Unix or Linux, try:

echo "text here" >> filename
command >> filename
date >> filename


printf "text here" >> file
date >> file
printf "\nTEXT HERE\n$(ls)" >> lists.txt

Display it using cat command:
cat lists.txt
You learned how to to add text to end of file in Linux or Unix-like systems using various command line options.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

4 comments… add one
  • Tina Dec 12, 2017 @ 20:52

    How do you append a new line to a text file followed by another Unix command?
    echo "LIST of files:" > file
    ls > file

    • 🐧 Vivek Gite Jan 14, 2019 @ 20:57

      Noop. try:
      echo "LIST of files:" >> file
      ls >> file

      Please use our forum if you need further assistance.

  • george Aug 2, 2020 @ 11:07

    Hi thanks for the tutorial. Please check this command:
    echo 'text' >>fileNameHere 2>>&1
    It gives me errors when I try it on my Ubuntu system!

    • 🐧 Vivek Gite Aug 2, 2020 @ 11:18

      It should be:
      echo 'text' >>fileNameHere 2>&1
      The page has been updated. Thanks!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.