Terminal 101: Join Multiple Files Together with Cat

Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application.

Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!

A few months ago, we covered the piping techniques that can be used to get text from a command and output it to a file on your Mac. This tool is great, but it also has a hidden feature when used along side of the cat command (short for concatenate): you can merge multiple files into one larger file. Continue reading, and we’ll have you concatenating files in no time.

To begin, you’ll want to have multiple files that you can concatenate together. Text files work great, but other file types are also supported, so play around with different filetypes.



We have three files: “sample1.txt,” “sample2.txt,” and “sample3.txt.” Each of these files contain text that we wish to join together in a single file called “sample-all.txt.” We’ll use the following command to make it happen:

cat sample1.txt sample2.txt sample3.txt > sample-all.txt

After the command runs, you will have a shiny-new “sample-all.txt” file containing the text of all of the files.



So, how does it work? Cat opens the file called “sample-all.txt,” and then writes each file, in order into the “sample-all.txt” file. When you open the file (say, in nano), you’ll see that the text from sample1 is written first, then sample2, and finally sample3.




Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.