Q:

How to Append Multiple Lines to a File from Terminal in Ubuntu

0

How to Append Multiple Lines to a File from Terminal in Ubuntu

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the echo Command

You can use the echo command to append multiple lines to a file using command line in Ubuntu.

For example, if your current working directory is /var/www and you want to append multiple lines to a text file named sample.txt inside of it, you can do something like this:

echo "Twinkle
Twinkle
Little Star" >> sample.txt

Alternatively, you can provide the absolute path of the file like this:

echo "Twinkle
Twinkle
Little Star" >> /var/www/sample.txt

Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash / is an absolute path.

You can also use the cat command to perform the same task like this:

cat <<EOT >> /var/www/sample.txt
Twinkle
Twinkle
Little Star
EOT

In Unix terminals, EOT (End-of-Transmission) is used to indicate the end of the data stream.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now