Redirection Reference (PAL Series)

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, I intended to keep all the basic/intermediate commands necessary for terminal use in one place to ease the students into using Linux efficiently. I’ve kept them up just in case they’re of use to someone.

Linux makes use of 3 standard streams

  • Standard input – stdin
  • Standard output – stdout
  • Standard error – stderr

By default these stream operate with the terminal; writing to stdout or stderr will display text in the terminal for example, and stdin will read from the terminal by default too.

However, it is often very useful to redirect these streams. For example, what if we wanted to run a program and store all it’s output in a text file?

$ ls > listOutput.txt     # Redirect stdout to listOutput.txt
$ cat listOutput.txt      # Print the contents
include
Makefile
README.md
shaders
src
tracer.pro

Using > to redirect will completely overwrite whatever data was in listOutput.txt.

If we wanted to append to the file instead, we can use the >> operator.

$ echo "I'm at the end" >> listOutput.txt     # Append the result of the echo command
$ cat listOutput.txt                          # Print the contents
include
Makefile
README.md
shaders
src
tracer.pro
I'm at the end

What if we wanted to ignore errors from a program? (assuming errors are being printed to stderr, as they should. Some programs don’t always adhere though)

$ goMaya 2> /dev/null   # Run Maya and redirect stderr to the special device /dev/null, which silently ignores all input

By redirecting to /dev/null, we’re no longer printing stderr to the terminal and it is basically a way of silencing a stream.

This also applies to the other streams, a common technique to completely silence a program and run it in the background is

$ dropbox >/dev/null 2>/dev/null &;
# or
$ dropbox >/dev/null 2>&1 &    # This achieves the same effect and basically says, redirect stdout to /dev/null, and redirect stderr to stdout

Or perhaps you want to run a program silently, but append all of it’s errors to a log file

$ renderManager >/dev/null 2>>logfile.txt

Piping

An alternative to redirection is piping, which works better with joining multiple commands together. A good explanation is here

$ cat Makefile | wc -l    # Pipe the output of the cat command into wc, using it to count the number of lines in the file
508

Here’s a really good example.

Summary

Stream Redirect symbol File Descriptor
stdin < &0
stdout > &1
stderr 2> &2

In the shell what is 2>&1?