next up previous
Next: Searching for Stuff Up: Introduction to the Linux Previous: More Shell Commands

Pipes and Redirection

Many commands take input from a place called standard input or stdin, and writes the output to standard output or stdout. By default stdin is the keyboard, and stdout is the terminal window.

Instead of leaving these as they are, we can redirect stdout to a file instead.

[phasnip@mijpnb1 ~]$ ls -C > list
[phasnip@mijpnb1 ~]$ cat list
a.out             ifort          minimiser.dvi   Nautilus     test.F90
castep_CVS.tar    linux.tex      minimiser.log   quality.sty  tmp
Desktop           list           minimiser.tex   Rules
hermes.addresses  minimiser.aux  minimiser.tex~  Teaching

If we redirected another command's output to this file it would overwrite it. Instead we could use » to append the result to the file.

[phasnip@mijpnb1 ~]$ ps >> list
[phasnip@mijpnb1 ~]$ cat list
a.out             ifort      minimiser.aux  minimiser.tex~  Teaching
castep_CVS.tar    junk       minimiser.dvi  Nautilus        test.F90
Desktop           linux.tex  minimiser.log  quality.sty     tmp
hermes.addresses  list       minimiser.tex  Rules
  PID TTY          TIME CMD
 2150 pts/1    00:00:00 tcsh
 2216 pts/1    00:00:33 emacs
 2638 pts/1    00:00:00 ps

In a similar way we can redirect stdin using the < symbol, though this is less common because it requires a file that contains all the correct responses to the commands requests.

By default commands run with stdout redirected will still write any error messages to the terminal.

[phasnip@mijpnb1 ~]$ ls -C rubbish > list
ls: rubbish: No such file or directory

This happens because error messages are not written to stdout, but to a third standard input/output device called stderr. We can redirect this to the same place as stdout using >& (or »&).

[phasnip@mijpnb1 ~]$ ls -C rubbish >& list
[phasnip@mijpnb1 ~]$ cat list
ls: rubbish: No such file or directory

There are many situations where what we really want to do is to take the output of one command as the input of another.

[phasnip@mijpnb1 ~]$ ls -R > list
[phasnip@mijpnb1 ~]$ more list
.:
a.out
castep_CVS.tar
Desktop
ifort
linux.tex
list
minimiser.aux
minimiser.dvi
minimiser.log
minimiser.tex
quality.sty
Rules
Teaching
test.F90

In this case the output was only redirected to a file so we could use the command more to view it a page at a time. The file is superfluous to our requirements really, all we need is a way to join the flow of output from one command to the input of another. This is possible using pipes, denoted by the vertical bar |.

[phasnip@mijpnb1 ~]$ ls -R | more
.:
a.out
castep_CVS.tar
Desktop
ifort
linux.tex
list
minimiser.aux
minimiser.dvi
minimiser.log
minimiser.tex
quality.sty
Rules
Teaching
test.F90

Pipes are extremely useful and powerful, because you can link as many commands together in this way as you like.


next up previous
Next: Searching for Stuff Up: Introduction to the Linux Previous: More Shell Commands
Phil Hasnip 2007-08-23