Linux append text to end of file

I am new Linux user and sysadmin. How to add lines to end of file? How do I append text to end of file using the cli on Linux?

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

For more info read redirection topic.

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 HEREn$(ls)" >> lists.txt

Display it using cat command:
cat lists.txt

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...