Standard streams and redirection in linux
In linux, we have 3 standard streams.
- Standard input stream – 0 – Keyboard
- Standard output stream – 1 – Shell terminal display
- Standard error stream – 2 – Shell terminal display
When we type any characters from the keyboard, it goes into the standard input stream. When program executes and displays output, it is pushed into the output stream (Terminal Display) . Any error information is pushed into the error stream (Terminal Display).
But we don’t want input or output to go into standard stream at all times. For example – input required for the program may be available in the file. So instead of reading from the keyboard(standard input stream – 0), we can read it from file itself. Similarly, output generated by the program may be redirected to file stream or any other stream instead of standard output stream (1 – Terminal Display).
We have below redirection operators in linux.
- > – redirects standard output to some file
- >> – redirects the output to the file (Appends)
- < – redirects standard input to some file
cat abc.txt > xyz.txt
Above command will copy data from abc.txt to xyz.txt
cat abc.txt >> xyz.txt
Above command will copy data from abc.txt and append it into xyz.txt
wc -l < xyz.txt
Above command will read data from xyz.txt and print total number of lines in that file.