[Chapter 5] Redirecting I/O
Author: Jerry Peek, Grace Todino & John Strang
Date: 1998-08-03T20:52:31Z

#

| Chapter 5 |

# 5. Redirecting I/O

Contents:
Standard Input and Standard Output
Pipes and Filters

## 5.1 Standard Input and Standard Output

Many UNIX commands read input (such as a file) and write output.

In general, if no filename is specified in a command, the shell takes whatever you type on your keyboard as input to the command (after you press the first `[RETURN]` to start the command running, that is). Your terminal keyboard is the command's standard input.

As a command runs, the results are usually displayed on your terminal screen. The terminal screen is the command's standard output.

So, by default, each command takes its input from the standard input and sends the results to the standard output.

These two default cases of input/output can be varied. This is called input/output redirection. You can use a given file as input to a command that doesn't normally accept filenames by using the "<" (less-than symbol) operator. For example, the following command mails the contents of the file to_do to bigboss@corp:

>     % mail bigboss@corp < to_do
>     %

You can also write the results of a command to a named file or some other device instead of displaying output on the screen using the `>` (greater-than symbol) operator. The pipe operator `|` sends the standard output of one command to the standard input of another command. Input/output redirection is one of the nicest features of UNIX because of its tremendous power and flexibility.

### 5.1.1 Putting Text in a File

Instead of always letting the output of a command come to the screen, you can redirect output into a file. This is useful when you have a lot of output that would be hard to read on the screen or when you put files together to create a bigger file.

As we've seen, the cat command can display a short file. It can also be used to put text into a file, or to create a bigger file out of smaller files.

#### 5.1.1.1 The > operator

When you add "> filename" to the end of a command line, the results of the command are diverted from the standard output to the named file. The > symbol is called the output redirection operator.

For example, let's use cat with this operator. The contents of the file that you'd normally see on the screen (from the standard output) are diverted into another file:

>     % cat /etc/passwd > password
>     % cat password
>     root::0:0:Root:/:/bin/sh
>     daemon:NONE:1:1:Admin:/:
>     	.
>     	.
>     	.
>     john::128:50:John Doe:/usr/john:/bin/sh
>     %

An example in Chapter 3, Your UNIX Account , showed how cat /etc/passwd displays the file /etc/passwd on the screen. The example above adds the > operator; so the output of cat goes to a file called password in the working directory. Displaying the file password shows that its contents are the same as the file /etc/passwd (the effect is the same as the copy command cp /etc/passwd password).

You can use the > redirection operator with any command that sends text to its standard output - not just with cat. For example:

>     % who > users
>     % date > today
>     % ls
>     password   today   users   ...

We've sent the output of who to a file called users and the output of date to the file named today. Listing the directory shows the two new files. Let's look at the output from the who and date commands, regarding these two files:

>     % cat users
>     tim     tty1    Aug 12  07:30
>     john    tty4    Aug 12  08:26
>     % cat today
>     Tue Aug 12 08:36:09 EDT 1997
>     %

You can also use the cat command and the > operator to make a small text file. We told you earlier to type `[CTRL-D]` if you accidentally enter cat without a filename. This is because the cat command alone takes whatever you type on the keyboard as input. Thus, the command:

> cat > filename

takes input from the keyboard and redirects it to a file. Try the following example:

>     % cat > to_do
>     Finish report by noon
>     Lunch with Xannie
>     Swim at 5:30
>     ^D
>     %

cat takes the text that you typed as input, and the > operator redirects it to a file called to_do. Type `[CTRL-D]` on a new line by itself to signal the end of the text. You should get a shell prompt.

You can also create a bigger file out of many smaller files using the cat command and the > operator. The form:

> cat file1 file2 > newfile

creates a file newfile, consisting of file1 followed by file2.

>     % cat today to_do > diary
>     % cat diary
>     Tue Aug 12 08:36:09 EDT 1997
>     Finish report by noon
>     Lunch with Xannie
>     Swim at 5:30
>     %

> CAUTION: If you are using the > (output redirection) operator, you should be careful not to overwrite the contents of a file accidentally. Your system may let you redirect output to an existing file. If so, the old file will be deleted (or, in UNIX lingo, "clobbered"). Be careful not to overwrite a much-needed file! Many shells can protect you from this risk. In the C shell, use the command set noclobber. The Korn shell and bash command is set -o noclobber. Enter the command at a shell prompt or put it in your shell's startup file After that, the shell will not allow you to redirect onto an existing file and overwrite its contents.

> This doesn't protect against overwriting by UNIX commands like cp; it works only with the > redirection operator. For more protection, you can set UNIX file access permissions.

#### 5.1.1.2 The >> operator

You can add more text to the end of an existing file, instead of replacing its contents, by using the >> (append redirection) operator. Use it like the > (output redirection) operator. So,

> cat file2 >> file1

appends the contents of file2 to the end of file1. For an example, let's append the contents of the file users, and also the current date and time, to the file diary. Then we display the file:

>     % cat users >> diary
>     % date >> diary
>     % cat diary
>     Tue Aug 12 08:36:09 EDT 1997
>     Finish report by noon
>     Lunch with Xannie
>     Swim at 5:30
>     tim     tty1    Aug 12  07:30
>     john    tty4    Aug 12  08:26
>     Tue Aug 12 09:07:24 EDT 1997
>     %

4.5 Printing Files |  | 5.2 Pipes and Filters