Redirecting Input and Output

The output from most commands is sent to standard output, usually the screen. You can, however, redirect output from a command
into a file instead of to your screen.

To redirect output to a file, you would type:

    <command> > <filename>

Some examples:

    man csh > shells
    cat red white > pink
    ls -R > directories

In the above examples, if the specified file already existed, it
would be overwritten. If you want to append output to a currently
existing file instead of overwritting it, you would type:

    <command> >> <filename>

For example:

    cat apple banana >> fruits

The input to most commands comes from standard input, usually the
keyboard. You can however, redirect input to a command to come from a
file rather than the keyboard.

To redirect input to a command, you would type:

    <command> < <filename>

Some examples:

    mail ttrojan@usc.edu < assign1
    matlab < grades
    spss -m < wages

You can also redirect standard output to a file while redirecting standard error to the screen by typing:

    <command> >& <filename>

For example, if you were running a C or fortran program and you
wanted the results to go to a file, but the error messages to go to the
screen, you could type:

    a.out >& results

Output Redirection

The previous section describes redirecting output to file or input
from a file. You can also redirect the output of one command into the
input of another command by using the | symbol– the “pipe” symbol. You would type:

    <command> | <command>

Some examples:

    man csh | lpr -Pps_ucc101
    printers | more
    sysinfo | mail ttrojan@usc.edu

You can also redirect the output of a command to a file and to the screen or another command using the
tee command. You would type:

    <command> | tee <filename>
    or
    <command> | tee <filename> | <command>

The first example will send a copy of the output to a file and a
copy of the output to the screen. The second example will send a copy
of the output to a file and send a copy of the output on to the next
command.

Some examples:

    ls -alR | tee mydir | lpr -Pps_ucc101
    last | tee stats | more
    df | tee usage