Episode 032 – cat

The cat command concatenates files or standard input to standard out by default. What this means is that the cat command will display the contents of a file to standard out so you can read it. The cat command is also very useful with redirection allowing you to combine multiple files together as, for example, with the split command. Recall that the split command divided a file into multiple smaller files. To restore the original file the cat command was used to cancatenate the smaller files back into one large file.

The usage of the cat command is quite simple. For instance, if we had a file called cat_test.txt with the following contents:

here is a cat test
I am adding a tab on the next line
there is a tab here
throw some blank lines in here
See how that goes

Issuing:

cat cat_test.txt

Will output the contents of the file to standard out:

here is a cat test
I am adding a tab on the next line
there is a tab here
throw some blank lines in here
See how that goes

There are a few switches that allow you to control the output of the contents. The -n, or –number, will number each line of output:

cat -n cat_test.txt

Produces:

cat screen shot with -n option

You can just number only the non-blank lines using the -b, or –number-nonblank, switch:

cat -b cat_test.txt

Produces:

cat screen shot with -b option

The -s, or –squeeze-blank, switch will remove repeated empty lines of output. This switch will not remove all blank lines, but only the second or more blank lines in a row. So in the example file the blank line after the fourth line is retained; but lines 6, 7, and 8 are “squeezed” out:

cat -sn cat_test.txt

Produces:

cat screen show with -sn options

To see the non-printing characters that are in a file use the -v, –show-nonprinting switch. The example file has been augmented with some dos non-printing characters. Catting the file now:

cat cat_test.txt

Produces

cat screen show no options

But:

cat -vn cat_test.txt

Shows the non-printing characters:

cat screen shot with -vn options

Note the ^M and ^V characters on lines 10 and 11 respectively.

The -E, or –show-ends, displays a “$” at the end of each line:

cat -E cat_test.txt

Produces:

cat screen show with -E option

To show tabs in a file use the -T, or –show-tabs, switch and tabs are replaced with “^I”:

cat -T cat_test.txt

Produces:

cat screen shot with -T option

There are a few aggregate switches that combine options:

  • -e combines -vE ( –show-nonprinting and –show-ends)
  • -t combines -vT ( –show-nonprinting and –show-tabs)
  • -a combines -vET ( –show-nonprinting, –show-ends, and –show-tabs)

If no file is specified with the cat command standard input is used. Similarly, using a “-” will signify to use standard input instead of a file:

cat

Issued like this by itself the cat command will echo back what is entered through standard input once the “enter” or “return” key is pressed.

The cat command has no pager functionality so all content that the command displays will scroll off the screen. Using a pipe, “|”, output from the cat command can be passed to a pager like less or more:

cat httpd.log | less

The cat command is a handy utility for viewing the contents of a file or redirecting the contents of one file into another. One example, culled from the Linux from Scratch documentation is to use cat as a quick and dirty text editor:

cat > test.sh << “EOF”

This example cats all standard in to test.sh until a single line containing “EOF” is listed. At which point cat closes the file and exits. Using cat like this or with other commands like cut, head, and tail one can perform some pretty sophisticated text manipulation.

Bibliography

  • man cat
  • info cat

If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much!

This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Episode 032 – cat

  1. Mat says:

    Hello,

    Good stuff but the last example did not work for me. Here is what i got:


    $ cat > test.sh < I am using cat as an editor
    > it is simple but works
    > this could be very useful
    > EOF
    > "EOF"
    > ^C
    $ cat test.sh
    cat: test.sh: No such file or directory

    What did I do wrong?

  2. Mat says:

    Hello,

    The first one did not paste correctly here is the output:

    $ cat > test.sh < I am using cat as an editor
    > it is simple but works
    > this could be very useful
    > EOF
    > “EOF”
    > ^C
    $ cat test.sh
    cat: test.sh: No such file or directory

  3. Mat says:

    Hello,

    It still is not posting correctly. The first line should be you command, then there should be a greater than sign in front of the I and it should be a new line.

    • dannSWashko says:

      Ok so you are putting this line in right:

      cat > test.sh << “EOF”

      When hitting enter you are presented with a blank line you can type in. All that you type will be catted to the file test.sh until there is a single line with just EOF on it. EOF has to be on the line by itself. Also make sure you are using << and not <.

      You can actually substitue just about anything for “EOF” like say “stop now”. But then to close this redirection and write to the file you have to have, on a line by itself, stop now.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>