Linux append text to end of file

ADVERTISEMENTS



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

Append text to end of file using echo command:

echo 'text here' >> filename 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 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



For more info read redirection topic.

Append text when using sudo

Try the tee command:

echo 'text' | sudo tee -a my_file.txt

echo '104.20.186.5 www.cyberciti.biz' | 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 echo "text here" >> filename command >> filename date >> filename

OR

printf "text here" >> file date >> file ## printf "

TEXT HERE

$(ls) " >> lists.txt printf "text here" >> file date >> file ## printf "

TEXT HERE

$(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.