Input and Output Redirection

If you want to use a file for command input or output instead of terminal input or output, you can do this very easily by I/O redirection.
Notation: stdinstandard input(keyboard)
stdoutstandard output(screen)
stderrstandard error(screen)

< Bourne, C stdin
> Bourne, C stdout
>> Bourne, C append stdout
<<str C (B) read input lines until str is encountered at the beginning of a line
>& (2>&1) C (B) stdout and stderr
>2 B stderr
>>& C append stdout and stderr
>! C stdout (overrides noclobber)
>&! C stdout and stderr (overrides noclobber)
>>! C append stdout (overrides noclobber)
>>&! C append stdout and stderr (overrides noclobber)

So - for example - the line

$ (find / -name "*pattern*" -print > /dev/tty) >& /dev/null

displays files that match pattern on the screen (even the terminal is represented by a file: /dev/tty) and sends error messages to /dev/null (that means: throws them away).

To prevent files from being overwritten: set the C shell variable noclobber!
(see below)


There's another possibility to manage a command's input and output: the possibility to connect the stdout of one command to the stdin of another without temporary files: the pipe (|):

$ cmd_1 | cmd_2 > out_file

In this example the command cmd_1 has no input from a file (but it may have input from the terminal) and sends its output to cmd_2, which uses it instead of terminal input. cmd_2 sends its output to the file out_file.

The following example shows how to get a listing of the subdirectories only:

$ ls -l | grep "^d"