-->
Previous Table of Contents Next


Using Cat

The cat command does so many things under the UNIX and Linux operating systems, it’s a wonder you don’t use it for everything. On a very basic level, cat can be used to view the contents of a file:


     gilbert:~$ cat filename

where filename is the name of the file you want to view. For example, to view the contents of a file named test, you’d use the following command line:


     gilbert:~$ cat test

     This is our Linux test file. Big whoop.

Cat, by default, displays its output to the screen. However, cat can be told to send its output elsewhere, which brings us to another of its many uses: It can also be used to store a file under a different filename, much in the manner of the cp command (which will be covered later in this chapter). For example, to create another copy of the test file (which we’ll call memo.kr), you’d use the following command line:


     gilbert:~$ cat test > memo.kr

     gilbert:~$ ls

     memo.kr     test

In this example, cat uses the output from the test file as the input for the memo.kr file.

Cat can also be used to create simple ASCII files; we say simple because cat sends your keyboard input directly to a file, rather than giving you the chance to edit the file. (The full-screen editors elvis and emacs can be used to edit files.) To create a simple file named memo, you’d use the following command line:


     gilbert:~$ cat > memo

Anything you type would go directly into the memo file one line at a time. When creating a file like this, there are a few things to remember:

  Hit the Enter key at the end of every line. Otherwise, part of your typing will end up in the ether.
  You can move within the line using the Backspace key (well, partially, anyway; Backspace will merely delete the preceding character). You can’t move to a previous line, however.
  Type Ctrl-D when you’re finished typing.


NOTE:  The Ctrl-D sequence can be used whenever you run a Linux command that requires keyboard input.

Finally, cat can be used to combine files. For example, you can add to the aforementioned memo file with the following command line:


     gilbert:~$ cat >> memo

Whatever you type will be added to the memo file. The previous rules apply. In addition, you can redirect two existing files as input to a new third file:


     gilbert:~$ cat memo1 memo2 > memo3

The order of the files on the command line determines the order of the data in the new file.

There are a host of options to the cat command; they are listed in Table 4.7.

Table 4.7 Options to the Cat Command
Option Result

-b Numbers all lines, except for those not containing characters.
-n Numbers all lines.
-s Replaces a series of blank lines with a single blank line.
-v Prints nonprinting (i.e., control) characters.

Other Ways to View a File

Linux contains two handy tools for viewing a file: more and less. The more command is pretty simple; the following command line launches more with the file test:


     gilbert:~$ more test

The more command presents one page of text at a time, with the percentage of text displayed at the bottom of the screen. Use the Enter key to move forward one line in the document, or press the Spacebar to move ahead an entire page. Unfortunately, you can’t move back to the beginning of a file once it’s scrolled by.

In addition, more gives you the ability to search for a specific text string, by typing:


     /string

where string is the text string you want to search for.

Where Less is More Than More

The less command isn’t part of the standard UNIX distribution, but it’s a very useful addition to the Linux command set. The less command provides more options when viewing a file—namely, the ability to move both forward and backward through a file. Again, to use less to view the file named test, you’d use the following command line:


     gilbert:~$ less test

As with more, you can use the /string option to search for text.

A big advantage to less is its ability to search backward through a file by pressing b.


NOTE:  An X Window version of this program, xless, is also included as part of Slackware.

Using Head and Tail to View Portions of a File

If a file is especially large, you may not want to load all of it and try to scroll through it, particularly if you’re just interested in a quick glance at its contents. In this case, you can use the head command to view the beginning of the fil, or tail to view the end of the file. For both commands, the default is to display 10 lines. Therefore, to display the first 10 lines of the file report, you’d use the following command line:


     gilbert:~$ head report

To view the last 10 lines of the file report, you’d use the following command line:


     gilbert:~$ tail report

To change the default of 10 lines, you’d specify a new number as an argument to the command; the following, for example, displays the first 20 lines of the file report:


     gilbert:~$ head -20 report

Viewing an Octal Dump with Od

Finally, there’s the od command, which allows you to view an octal dump of a file:


     $ od filename

where filename is the name of the file to be viewed.


Previous Table of Contents Next