UNIX Tutorial


9. Redirecting input and output

Commands covered in this section:   mail

For every process in UNIX, three files are automatically opened: standard input, standard output, and standard error. (For the purposes of this section of the tutorial, you can think of a process as a program you run from the shell, such as ls or who.) Standard input (sometimes abbreviated as stdin) is usually mapped to the keyboard. This means that the process will look for its input from the keyboard by default. Standard output (sometimes abbreviated as stdout) is usually mapped to the screen. By default, the process will send its normal output to the screen. Standard error (sometimes abbreviated as stderr) is similar to standard output, except that it is reserved for error messages.

You can use redirection to send the input or output of a process to a location other than the standard one. The "greater than" symbol ('>') is used to redirect the standard output of a process. By placing '>' after a command and following this with the name of a file, the output of the command will go to the specified file instead of the screen.

Exercise 9.1

Repeat the who command covered in section 7 and redirect the output to a file.

$ who > who.out
$ cat who.out
natasha    pts/2        Jan 22 08:59    (foo.wossamotta-u.edu)
boris      pts/3        Jan 22 09:27    (bar.wossamotta-u.edu)
rocky      pts/8        Jan 21 23:00    (quux.wossamotta-u.edu)

The order here is important: you need to enter the name of the command producing the output first, then the '>' character, then the name of the file that will contain the output.

It is not necessary to create the output file ("who.out" in the example above) before doing the redirection. It will be created automatically when the command is run. Note that if the file does exist before the redirection takes place, using '>' will cause any existing contents in the file to be overwritten. In other words, if you have a file called "myfile" with something important in it (let's say it's your doctoral dissertation), you may want to think twice before running a command like "who > myfile". Some systems are configured to prevent this (it's called "noclobber") by default. In that case, the redirection will fail and you will receive an error message saying the file exists.

If you would like the output of a program to be appended to a file, as opposed to overwriting it as discussed above, you can use '>>' to redirect the output to a file.

Exercise 9.2

Repeat the who command once again, this time using '>>' to redirect the output.

$ who >> who.out
$ cat who.out
natasha    pts/2        Jan 22 08:59    (foo.wossamotta-u.edu)
boris      pts/3        Jan 22 09:27    (bar.wossamotta-u.edu)
rocky      pts/8        Jan 21 23:00    (quux.wossamotta-u.edu)
natasha    pts/2        Jan 22 08:59    (foo.wossamotta-u.edu)
boris      pts/3        Jan 22 09:27    (bar.wossamotta-u.edu)
rocky      pts/8        Jan 21 23:00    (quux.wossamotta-u.edu)

Now, you have two copies of the output from who: the first was placed there when the file was created with the redirection using '>', and the second was appended using '>>'.

As you might expect, the "less than" ('<') symbol is used to redirect the standard input of a process. By placing '<' after a command and following this with the name of a file, the input for the command will be taken from the specified file instead of the keyboard.

Exercise 9.3

Take the output from the who command which was redirected to a file in the previous exercise and mail it to yourself with the mail command.

$ mail -s "redirection" username@domain.com < who.out

Again, note that the order is important. Here, the command is 'mail -s "redirection" username@domain.com'. This is followed by the input redirection symbol ('<'), then the name of the file containing the input to be sent to the program. This should cause the contents of the file to be mailed to the address you specified, with the subject heading set to "redirection."

If you have trouble figuring out which redirection symbol to use, just think of the '<' and '>' symbols as arrows. If the arrow is pointing from a filename to the program (as in "mail joe@domain.com < myfile"), you are redirecting the input. If the arrow points from the program to a filename (as in "who > myfile"), you are redirecting the output.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.