Linux append text to end of file

last updated in Categories , ,

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

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

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.

Posted by: Vivek Gite

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.