Command line tips & tricks - concatenation with cat

If you've done any sort of command line stuff on a Unix-like OS, you've probably come across cat. It stands for 'concatenate', but most often we use it to simply spit out the contents of a file, like this:

$ cat myfile

So hang on - why is it called concatenate if we're only showing the contents of one file? Well, if you give cat one file, it just does that.

As the name suggests however, you can give cat two files and it will - guess what - concatenate them together and throw them out at the end.

Like this:

$ cat fileone filetwo

There, you'll be given an output containing the contents of both files. Why is this useful? It might not seem it, but there are a number of situations where this could help you.

Say you have a couple of files, and you want to search for a particular phrase in both. You could cat them together, and grep the result for the thing you're searching for, like so:

$ cat fileone filetwo | grep 'searching for this'

This example wouldn't tell you which file the results was, but it's just a nice and simple example. How about another?

Say you have a compressed file that has been split into 600 MB chunks (for burning to CD, for example). You can download all the chunks, then cat them together (instead of sending the output to the screen, we redirect it to a finished file that you can then decompress).

$ cat part1 part2 part3 part4 > completefile

Finally, here's a really neat trick, and that's how to use cat as a very basic text editor. Type in the following into your terminal:

$ cat > newfile

After hitting enter to run the command, type in some text you want to save into the new file. When you're done, press Ctrl-D and cat will write that text to the file. Neat, huh?

Have you got an awesome tip or trick you want to share with FOSSwire readers? It could be anything, provided that it's got something to do with free and open source software. Email them to peter at gizbuzz .co dot uk!

Avatar for peter Peter Upfold -

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command line tips & tricks - concatenation with cat

  1. # Posted on 27 March 2007 at 02:13 PM

    [...] is the second time I have written a blog post outside. The first was this FOSSwire post, that I composed just a few moments [...]

  2. Rahul Batra (guest)

    # Posted on 28 March 2007 at 11:33 AM

    Instead of doing: $ cat fileone filetwo | grep "searching for this" The following is much better/efficient: grep "searching for this" fileone filetwo

    (although the latter does not demonstrate the use of cat in our case)

  3. fedoraphile (guest)

    # Posted on 31 March 2007 at 08:57 AM

    split is a great command to consider alongside cat. While cat will allow you to reassemble a file that has been split into chunks, split allows you to split a file into chunk sizes you designate (when archiving to cd or dvd media, for example).

Home » Articles » Command line tips & tricks - concatenation with cat