Americas

  • United States
sandra_henrystocker
Unix Dweeb

Shortcuts for adding multiple lines of text to files on Linux

How-To
Nov 29, 20216 mins
Linux

It's easy to add a few lines of text to files on Linux, but there are tools that make it easier to add many lines.

a hand holds an open book from which abstract letters emerge
Credit: EFKS / Getty Images

There are a number of ways to add text to files on Linux systems without having to open an editor, such as the echo and printf commands. On the other hand, when you need to add A LOT of text, the technique you use can make the job tedious or a piece of cake.

This post describes some of the commands you can use and the problems and benefits you can expect.

Also see: How to loop forever in bash on Linux

The echo commands append lines of text to existing files in a straightforward way:

$ echo "text to be added to file" >> myfile
$ echo "more text" >> myfile

You can also use the printf command to append text, but keep in mind that by default it will not add a newline as if you hit the Enter key. Use a command like this with the n to include it:

$ printf "appended textn" >> myfile

When you want to build a script that will add a series of lines to a file, the echo and printf methods just shown can get a bit tiresome. One option is to add multiples lines with a single echo or printf command that includes newline characters like this:

$ echo -e "text to be added to filenmore text" >> myfile
$ printf "more text to be addednand so onnand so onn" >> myfile

The -e option makes the echo command interpret the n sequences as newlines, so your file will include these lines:

$ tail -5 myfile
text to be added to file
more text
more text to be added
and so on
and so on

You can also just type each line separately as long as you don’t end the quote until the end of the text. This type of multi-line echo requires that you start your echo command on one line with an opening double-quote (“), add quote-free lines and then close the echo command on the last line with a closing double-quote. Here’s an example of doing this on the command line:

$ echo  "There is more than one way to skin a cat
> but skinned cats are never very cuddly.
> You would be better off skinning a catfish and
> likely less inclined to cuddle it afterwards." > cats
$ cat cats
There is more than one way to skin a cat
but skinned cats are never very cuddly.
You would be better off skinning a catfish and
likely less inclined to cuddle it afterwards.

If you want to add a large amount of text, however, even the multi-line echo command above can require more effort than it’s worth. Two other options are 1) putting the text into a separate file and appending the content of that file to the one you’re updating, and 2) adding the text using a script.

Adding text using a separate file

To add text from one file to the end of another, we could first create the file to be appended. Here’s an example using the addition of a warranty disclaimer to the end of a file detailing a product:

$ cat warranty_disclaimer
WARRANTY DISCLAIMER
===================
The information provided about this product is not intended to provide any
promise that it will work as advertised or that it will continue to work
for any particular period of time. The seller accepts no responsibility for
how the buyer uses it nor for any damages caused during or by its use.

Once the text file is ready, run a command like this to append it:

$ cat warranty_disclaimer >> cordless_drill

In this and earlier examples, you need to be careful not to overwrite the target file by using a single “>”.

Adding text using a script

Another option is to add the lines using a script. For this, you can use a multi-line echo command like one of those shown above.

The example script below uses this technique to add the warranty disclosure to any file provided as an argument to the script or provided at the prompt:

#!/bin/bash

if [ $# != 1 ]; then
  echo -n "file name> "
  read filename
else
  filename=$1
fi

if [ ! -f $filename ]; then
  echo "No such file: $filename"
  exit
fi

echo "
WARRANTY DISCLAIMER
===================
The information provided about this product is not intended to provide any
promise that it will work as advertised or that it will continue to work
for any particular period of time. The seller accepts no responsibility for
how the buyer uses it nor for any damages caused during or by its use.
" >> $filename

If you have a file that ends like this:

$ tail -2 cordless_drill
Use this product to drill holes in things. Be careful not to drill holes in
your pets or fragile objects that might break.

You can add the disclaimer like this:

$ add_disclaimer cordless_drill

Verify the additional lines like this:

$ tail -10 cordless_drill
Use this product to drill holes in things. Be careful not to drill holes in
your pets or fragile objects that might break.

WARRANTY DISCLAIMER
===================
The information provided about this product is not intended to provide any
promise that it will work as advertised or that it will continue to work
for any particular period of time. The seller accepts no responsibility for
how the buyer uses it or for any damages caused during or by its use.

One benefit of this approach is that you can easily insert your text into the script without adding commands. Just put it between the first echo and the redirection line.

Note that the script shown above includes checks to see that a file name was specified and, if not, prompts for one. It also checks to ensure that the specified file exists.

Scripts like this are a good option whenever you need to append lines of text to a lot of files.

Wrap-Up

There are many ways to append text to files on Linux, but to add a lot of lines, a script is likely one of your better options, especially if you need to do this repeatedly. You can use a script that includes the multi-line echo command as detailed above to add any amount of text to files.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author