Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.
We may want a file to be the input for a command that normally wouldn’t accept a file as an option. This redirecting of input is done using the “<” (less-than symbol) operator.
Below is an example of sending a file to somebody, using input redirection.
> mail [email protected] < sample_ddl
This reads a bit more difficult than the beginner’s cat file | mail someone, but it is of course a much more elegant way of using the available tools.
File Descriptors (FD)
In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).
Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed.
Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error.
File | File Descriptor |
Standard Input STDIN | 0 |
Standard Output STDOUT | 1 |
Standard Error STDERR | 2 |
Following is a complete list of commands which you can use for redirection −
Sr.No. | Command & Description |
---|---|
1 | cmd > file Output of cmd is redirected to file |
2 | cmd < file Program cmd reads its input from file |
3 | cmd >> file Output of cmd is appended to file |
4 | n > file Output from stream with descriptor n redirected to file |
5 | n >> file Output from stream with descriptor n appended to file |
6 | n >& m Merges output from stream n with stream m |
7 | n <& m Merges input from stream n with stream m |
8 | << tag Standard input comes from here through next tag at the start of line |
9 | | Takes output from one program, or process, and sends it to another |