Pin Me

Linux Command Line: cat

written by: jlwallen•edited by: Michele McDonough•updated: 11/29/2008

Picture this: You have two text files you need to merge into one so you open both documents and copy paste the contents of one into the other. Imagine being able to do this without having to open a single file. That is what cat does. This article will show you how to merge two files with cat.

  • slide 1 of 4

    What cat does

    In Linux, cat stands for concatenate. What this gem of an application does is take input from the command line and outputs the content to either the screen or to a file. Here's a simple example.

    File1.txt contains the contents "To be or not to be".

    File2.txt contains the contents "That is the question".

    To merge these into File3.txt that contains:

    To be or not to be

    That is the question

    the command cat File1.txt File2.txt > File3.txt would need to be issued.

    Or, you could just have the contents of both files displayed on the screen with the command cat File1.txt File2.txt.

    As a Linux beginner it might not be obvious how useful this tool is. But once you get deep into using the Linux OS, writing bash scripts, programming applications, etc. you will see how much cat can do for you.

  • slide 2 of 4

    Piping cat to grep

    Say you want to cat two files together while searching both texts for certain words. Using our example above, we'll do a search for the word "be". To do this you would pipe cat to grep like so:

    cat -b File1.txt File2.txt |grep be

    This would display the following results:

    1 To be or not to be

    So you know the word "be" shows up twice, both times in line 1.

  • slide 3 of 4

    Helpful arguments

    Here is a listing of some of the handier arguments to use with cat.

    b - number the non-blank output lines

    n - number all output lines

    s - squeeze multiple adjacent blank lines

    v - display non-printing characters (except for tabs and end of line characters)

    t - same as v but also displays tabs (shown as "^I")

    e - same as v but also diplays end of line characters (shown as "$")

  • slide 4 of 4

    Final Thoughts

    Although you have only been shown a basic sampling of what cat can do, you should (with a bit of imagination) see how useful this tool can be. The cat command is one of those tools you will find at the command line, in bash scripts, in configuration files, and in a whole host of other locations. Get to know cat. The time you spend learning it will be time well spent.