Linux offers many commands to work with files in shell. Sometimes you may need to append one file to another in Linux. In this article, we will learn how to merge or combine two or more files into a single file. There are several ways to append one file to another in Linux. We will be using the simplest operator (>>) for this purpose. It is available on almost every Linux distribution.
How to Append One File to Another in Linux
Linux offers >> operator to help you append content of one file to another. Here is an example to append file f2 to file f1.
$ cat f2 >> f1
Please note, you need to have read/write permission for both files f1 and f2 for the above command to work properly. Otherwise, you will get ‘permission denied’ error. In such cases, use sudo keyword at the beginning of the command to make it work.
$ sudo cat f2 >> f1
If the destination file f1 does not exist, then >> operator will create a blank new file and append contents of f2 to it.
You can also concatenate multiple files into a single file using wildcard characters. Here is an example to concatenate all *.txt files into single file f1.
$ cat *.txt >> f1
If you want to regularly append the contents of a file to another file, you can add the above command as a cronjob. Open crontab for this purpose.
$ crontab -e
Add the following line to append f2 to f1, everyday at 10.a.m.
0 10 * * * cat f2 >> f1 >/dev/null 2>&1
Save and close the file. In this article, we have learnt how to add contents of one file to another.