Unix File Redirection and Piping Redirection
Unix commands can be strung together to form a powerful assembly line.
When commands are involved, the process is called "piping." When
files are involved, it's called "redirection." Commands covered are:
- | (pipe)
- < (redirect input from file)
- > (redirect output to file)
- >> (redirect output and append to file)
- tee (redirect output to file AND pipe)
The file redirection facility in Unix allows a program to access a
single input file and a single output file very easily. The program
is written to read from the keyboard and write to the terminal screen
as normal. The program is run slightly differently in order to access
files for input and output.
To run prog1 but read data from file infile instead of the keyboard,
you would type
prog1 < infile
To run prog1 and write data to outfile instead of the screen, you
would type
prog1 > outfile
Both can also be combined as in
prog1 < infile > outfile
Redirection is simple to achieve, and has the advantage that the same
program can be used to read or write data to or from files or the
screen and keyboard.
Some programs require several files to be used for input or output,
and redirection cannot do this. In such cases you will have to use
C's file handling facilities.