Piping and Redirection

There are STDIN(0), STDOUT(1) & STDERR(2):

Stream NameDescription
Standard Input (STDIN)Data fed into the program
Standard Output (STDOUT)Output from the program (defaults to terminal)
Standard Error (STDERR)Error messages (defaults to terminal)

We can use |< or > to pass any of the 3 streams to other programs:

Redirecting to a New File

  • echo "test" > file.txt
    • if the file already exists it is overwritten!

Redirecting to an Existing File

  • to append to a file use >>
    • echo "test" >> file.txt

Redirecting from a File

  • echo < file.txt

Redirecting STDERR

  • ls ./none-existent-file.txt 2>error.txt

Piping

  • cat error.txt | wc -m

Relevant Note(s): Linux Basics