Working with files and directories

Specifying command input and output

Almost all UNIX commands require an input and an output; that is, some information to read and process, and somewhere to store the results. If you do not tell a command where to find its input and output, it makes assumptions about where to read and write information. These assumptions are called the standard input and standard output. These are, respectively, your keyboard and your screen by default. Alternatively, if you specify the name of a file, most programs will obtain their standard input by reading the file.

In addition to standard input and standard output, most programs need a standard error, to which they report any failures or errors. By default, the standard error is directed to the same place as the standard output, your screen.

You can make commands redirect their standard input and output by using the symbols ``<'' and ``>'' on the command line, followed by the name of a file to read input from or write output to. For example sort < list1 > list.out makes sort treat list.1 as its input, and send its output to list.out.

If you send the output of a program to a file, and the file already exists, the existing file will be ``clobbered'' or overwritten. If you are using the Korn shell, you can prevent this from happening by using the noclobber variable. You can identify your login shell by entering the following command:

   $ grep ${LOGNAME} /etc/passwd
   martins:x:13990:1014:Martin Smith:/u/martins:/bin/ksh
The last data field, after the last colon, identifies your login shell, in this case, the Korn shell (/bin/ksh). If you are using the Korn shell, you can turn on the noclobber feature, by typing the following:
   $ set -o noclobber
C shell users should type set noclobber: this feature does not exist in the Bourne shell. To turn off the feature in the Korn shell, type set +o noclobber. C shell users should type unset noclobber. To protect yourself from accidentally clobbering files, Korn shell users should add the appropriate noclobber line to your .profile file: C shell users should add it to .cshrc.

To append the output of a command onto the end of a file, use the >> notation instead of >. For example, the following command line appends the output from sort to the end of the existing contents of file2 rather than overwriting them:

   $ sort file1 >> file2
The C shell will not let you append the standard output to a file if the file does not exist and noclobber is set. The Bourne or Korn shells simply create the file.

Typically, your shell will support a wider range of redirection operators than those discussed here. For details, refer to ksh(C), sh(C) or csh(C) as appropriate. See also ``More about redirecting input and output''.

Next topic: Forcing a program to read standard input and output
Previous topic: Cleaning up your filesystem

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005