There are several ways to append text or new lines to a file using the Bash command line. The majority of them use the double >> to redirect output from another command (such as “echo” or “printf“) into the designated file. An alternative method, using “tee“, can be used when your new text is already available from another input source – usually another text file.

Whenever you use any of the redirection methods, always take care not to mistype a single arrow > when you need a double one >>. Both will redirect your output, but the former will overwrite the file’s existing content instead of appending it. There is no undo when you are issuing shell commands in real-time!

Whichever method you use, remember that you always need system write permissions for the target file. Another common error to avoid is misspelling the target file name. If the file you specify to receive output does not exist, Bash creates it for you and then fills it with the new output. You will then have two files with similar names but neither containing all the lines that you intended.

Using the echo command

The echo command sends your subsequent output to the standard output destination – which is usually your screen, but you can also use it to send the same output to the end of an existing file. For example, type the following.

$ echo "Hello" > file.txt
$ echo "World" >> file.txt

If “file.txt” didn’t already exist you have now created it and filled it with two lines of text. To see the file’s content, use the “cat” command;

$ cat file.txt

This will send the two lines in your file to the screen –

Hello
World

You can also use “echo” with the –e argument telling it to recognize the escape character and following control code such as “\n” for a new line. This allows you to fill your output file with multiple lines instead of having to repeat another echo command for every line.

$ echo -e "Hello\nWorld"
Hello
World

However, when you need to enter numerous lines, it is often more practical to use one of the other commands or the “tee” method described below.

Using the printf command

The “printf” command can be used in a very similar way to “echo” but makes it easier for you to use arguments to create a more complex output. For example, if “bob” is your logged in user name –

$ printf "Hello, I'm %s." $USER >> file.txt

This would create a file ending with the line –

Hello, I'm bob.

Using the cat command

The “cat” command is often used to output the content of a file to the screen, but it can also be used to concatenate several file sources and output them to another file. It is appropriate to use “cat” whenever you have numerous lines stored in a file, or in numerous files, that you want to append to your output file.

For example, you could send the contents of a series of files into a single destination file thus;

$ cat file1.txt file2.txt file3.txt file4.txt >> output.txt

You can also operate on your file contents while you perform this operation. For example, the following code will put all the lines from the source files into alphabetical order before entering them into the output file;

$ cat file1.txt file2.txt file3.txt file4.txt | sort >> output.txt

Other command redirections

The same rules can be applied to almost any command that will deliver a useful output to you, for example;

ls -al >> file.txt
find -type f >> file.txt
date +"Year: %Y, Month: %m, Day: %d" >> file.txt

Using the tee command

The Bash “tee” command has been written to read data from the standard input and write it to the standard output as well as to a file, or multiple files.

Because you aren’t using the >> operator, it is necessary to remember that the default behavior reverts to overwriting the destination file. To prevent that, we apply an “-a” option. The “a” stands for “append” which makes it easy to remember. Here are two simple examples of its use;

tee file1.txt –a output.txt
echo "another line of stuff" | tee -a output.txt

Remember that “tee” is sending the data to your standard output, the screen, as well as to the specified file. To suppress the echo you can add a redirect to /dev/null like this;

$ echo "another line of stuff" | tee -a output.txt >/dev/null

You can use “tee” in ways that aren’t possible with redirect operators. One is to append your string to multiple files at the same time. Another is when you need to append to a file owned by a different user – for which you use sudo. Here is an example that does both of those things;

$ echo "another line of stuff" | sudo tee -a output1.txt output2.txt output3.txt >/dev/null

One of the great features of Bash is that it is standard in every version of Linux and can also be used in Unix, Solaris, macOS (since Mojave), and even Windows X. Bash isn’t just for programmers – it helps you take back control of your computer.

 

Leave a reply

 

Your email address will not be published.