# NSCI 580A4 fall 2017

### Sidebar

NSCI 580A4

Instructors
Tai Montgomery
Erin Nishimura

wiki:2016redirect

# Redirection

## Standard Streams

Standard streams are default input and output channels. In Linux, there are three standard streams: stdin, stdout, and stderr. You may also see input and output channels referred to as Input/Output or simply I/O.

Standard Input, or stdin, is input going into the shell. By default, this input comes from you typing on the keyboard.

When your run a command, say wc file1.txt, the shell sends its output to a special file called standard output (aka stdout), which by default is not saved in a directory, but is routed to the screen.

If you run an errant command, say wc werkejtkhgo when there is no file werkejtkhgo, an error message will be produced. The error message goes to a file called standard error (aka stderr) that is also routed to the screen by default.

Redirection allows us to circumvent the defaults, allowing us to redefine where standard output and standard error go. This is a useful way to capture information.

## Redirect output with >

Redirection of standard-output is performed using the > operator.

Redirect stdout Usage:
command line > outputfilename.txt

$wc rna_coding_R64-2-1_20150113.fasta > wc_output.txt ## Redirect error and status messages with 2> Let's see what happens if we tried to redirect an errant command… $wc blerg > wc_fake_output.txt

We can capture the error message with… 2>. This redirects standard-error to our desired file:

Redirect stderr Usage:
command line 2> outputfilename.txt

$wc blerg 2> wc_err_output.txt ## Redirect both output and error/status messages with &> Wonderful! But what if we had given wc two files, one good and one bad. Experiment a little. $wc rna_coding_R64-2-1_20150113.fasta blerg > wc_both_1.txt
$wc rna_coding_R64-2-1_20150113.fasta blerg 2> wc_both_2.txt We can capture both using &>: Redirect stdout and stderr Usage: command line &> outputfilename.txt $wc rna_coding_R64-2-1_20150113.fasta blerg &> wc_both_3.txt

Common pitfall: redirection will overwrite existing files. If, instead, you would prefer to append the new information to the end of an existing file, you can use »