UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.1 Cracking the Nut Chapter 25
Showing What's in a File
Next: 25.3 Using more to Page Through Files
 

25.2 Four Ways to Skin 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 (13.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 the more (25.3) command or some other pager, like less (25.4). [1]

    [1] You may think this command form is pointless. The truth is, 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 (1.3).

  2. Second form:

    % cat files > 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. Articles 13.13, 25.7, 25.10, and 25.21 give some more useful tips about cat options.

- DG


Previous: 25.1 Cracking the Nut UNIX Power ToolsNext: 25.3 Using more to Page Through Files
25.1 Cracking the Nut Book Index25.3 Using more to Page Through Files

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System