-->
Previous Table of Contents Next


Standard Input/Output and Redirection

The third piece in the Linux puzzle concerns linking commands and files in the form of standard input and output (I/O). Don’t be dismayed by this techie term: standard I/O really concerns how command lines are structured and where the results of a command should be sent.

Linux is like every other operating system in that it needs to know where input is going to come from and where output should be sent. Other operating systems, such as Windows and the Macintosh, make assumptions when it comes to input and output. And in some circumstances so does Linux. But in most other circumstances, you need to put some thought into where your work comes from and where it goes. The basic principles behind standard I/O can be best explained with an example.

The cat command is an amazingly versatile command. It can be used to display the contents of files, add to files, and more. It can be used as a rudimentary text editor when run on a command line by itself:


$ cat

The cursor will go to the next line, and then you can enter text. Because you have not specified where the text should go, Linux and the cat command assume that the input should go to the screen. After the input goes to the screen, it’s lost forever, as there’s no mechanism for saving the text to disk. Most Linux commands assume that standard input means input from the keyboard, and standard output means display on the terminal. Under these circumstances, cat can be used for improving your typing skills, but otherwise it’s of little use.

However, cat’s usefulness increases when youcombine it with filenames in a command line. Combining cat and a filename displays an existing file on the screen, as shown in Figure 1.6.


Figure 1.6  Combining cat with a file.

Instead of using standard input from the keyboard, cat uses standard input from a file. The contents of the file haven’t been altered; they’ve merely been displayed on the screen.

You can use cat to store keystrokes to a file with the use of redirection symbols. These symbols, which are part of the core operating system, are used to alter standard I/O. You could combine cat—or any other Linux command, for that matter—with a redirection symbol to redirect its output to a file. In the following example, the output from cat, which is normally sent to the screen, is instead redirected to a file named kevin.report:


$ cat > kevin.report

The output is sent one keystroke at a time to the file kevin.report. Typing Ctrl-D stops the redirection and ends the cat command.

Redirection can be used both for input and output. The cat command can be told to copy a file to a new filename in the following manner:


$ cat kevin.report > pat.report

Here, the input is kevin.report, and the output is pat.report. Nothing about kevin.report is changed.

There is a separate redirection symbol for appending text to an existing file. Here, the contents of kevin.report are appended to an existing file named pat.report:


$ cat kevin.report >> pat.report

If you were to omit a filename as the input, cat would assume that keystrokes should be used for appending. The following command line lets you append keystrokes directly to the end of the file named kevin.report:


$ cat >> kevin.report

There are actually four redirection symbols:

  > is used to send the output of a command to a file or another command. For example, cat > file is used to send the output of the cat command to file.
  < is used to send the input of a file or command to a command. For example, cat < file means that cat should use file as input.
  >> is used to append input to an existing file. For example, cat >> file tells Linux to append the keystrokes to an existing file named file.
  | is the pipe symbol. It’s used when you want to run a command and then send its output to another command. For instance, cat | grep runs the cat command and sends the output to the grep command, which is then run. (We’ll cover pipes later in this chapter.)

When you look at these symbols, it may seem that there are a few different ways to do the same thing. Indeed, < and > are interchangeable, depending on how the command line is structured. However, both symbols are needed. Command lines that look similar can actually be dealt with quite differently by the operating system. For instance, this command line:


$ cat pat.file

is functionally the same as:


$ cat < pat.file

The two command lines are actually different, however. In the first, pat.file is an argument for the cat command. In the Linux world, arguments are command-line modifiers that are variables; in this instance, the argument happened to be a file. In the second example, pat.file is input to the cat command.

There is no limit to the complexity of command lines when it comes to redirection symbols. It’s not uncommon to see two redirection symbols used as follows, especially in a shell script:


$ cat < file1 > file2

This tells cat to use input from file1 and send the output to file2.


Previous Table of Contents Next