home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

12.2. What Good Is a cat?

The cat command may well be the first command new users hear about, if only because of its odd name. cat stands for concatenate or, as some would say, catenate. Both words mean the same thing: to connect in a series. The cat command takes its filename arguments and strings their contents together. Essentially, cat takes its input and spits it out again.

cat has many uses, but the four most basic applications are described in the following list. In many ways, they don't illustrate cat so much as they illustrate the shell's output redirection (Section 43.1) mechanism.

  1. First form:

    % cat file
    % cat file1 file2 file...

    Use this form to display one or more files on the screen. The output doesn't pause when the screen is full. As a result, if your files are more than one screenful long, the output will whiz by without giving you a chance to read it. To read output by screenfuls, use a pager such as less (Section 12.3).[43]

    [43]You may think this command form is pointless. In truth, this form is rarely used in such a basic way. More often, you'll use this form along with some of cat's display options or connect this command to other Unix commands via a pipe (Section 1.5).

  2. Second form:

    % cat file(s) > new_file

    Use this form when you want to combine several smaller files into one large file. Be sure the destination file does not already exist; otherwise, it will be replaced by the new contents (effectively destroying the original). For example, the command:

    % cat chap1 chap2 chap3 > book

    creates a new file, book, composed of three files, one after the other. The three component files still exist as chap1, chap2, and chap3.

  3. Third form:

    % cat file >> existing_file
    % cat files >> existing_file

    Use this form to add one or more files to the end of an existing file. For example:

    % cat note1 note2 > note_list
    % cat note3 >> note_list
  4. Fourth form:

    % cat > newfile

    Use this form as a quick-and-dirty way to create a new file. This is useful when you aren't yet familiar with any of the standard text editors. With this command, everything you type at the keyboard goes into the new file. (You won't be able to back up to a previous line.) To finish your input, enter CTRL-d on a line by itself.

Well, that was just in case there are some beginners on board. Section 12.4, Section 12.7, and Section 12.13 give some more useful tips about cat options.

-- DG



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.