Standard streams and redirection in linux

In linux, we have 3 standard streams.

  1. Standard input stream – 0 – Keyboard
  2. Standard output stream – 1 – Shell terminal display
  3. 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.

  1. > – redirects standard output to some file
  2. >> – redirects the output to the file (Appends)
  3. < – redirects standard input to some file

Examples –

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.

Tee command redirects output to the file as well as sends data in output stream
/dev/null is a special device. If you try to to redirect your data in that device, nothing will be stored.

You may also like...