Redirection

As seen briefly in the Module Finding Things it is possible to redirect the output of a command. In this module we will discuss this in more detail.

Redirecting output to a file

Rather than having the output of a command printed to the screen, we can send it to be written to a file instead.

[user@host ~]$ echo hello > hello.txt
[user@host ~]$ echo hello again >> hello.txt
[user@host ~]$ cat hello.txt
hello
hello again

>> appends the output to the end of an existing file.

> will overwrite any existing content.

File inspection revisited

[user@host ~]$ cd shell-training/animals
[user@host animals]$ head -n 5 mammals.txt
common pipistrelle, pipistrellus pipistrellus
whiskered bat, myotis mystacinus
natterer's bat, myotis nattereri
daubenton's bat, myotis daubentonii
leisler's bat, nyctalus leisleri

Use an option -n with head and tail to print n lines from the start or end of a file

What about printing a number of lines from the middle of a file? There’s no mid command!

An inefficient solution:

[user@host shell-training]$ head -n 15 mammals.txt > temp.txt
[user@host shell-training]$ tail -n 5 temp.txt
european hedgehog, erinaceus europaeus
pygmy shrew, sorex minutus
wood mouse, apodemus sylvaticus
house mouse, mus domesticus
brown rat, rattus norvegicus

What if you wanted to do this for 1000 files? What if your work flow involves several intermediate steps? That’s a lot of temporary files!

A better solution

We can use a pipe to redirect the output from one command and make it the input for another command:

[user@host shell-training]$ head -n 15 mammals.txt | tail -n 5
european hedgehog, erinaceus europaeus
pygmy shrew, sorex minutus
wood mouse, apodemus sylvaticus
house mouse, mus domesticus
brown rat, rattus norvegicus

sed can be used to find and replace words in text

[user@host shell-training]$ head -n 15 mammals.txt | tail -n 5 | sed 's/mouse/elephant/g'
european hedgehog, erinaceus europaeus
pygmy shrew, sorex minutus
wood elephant, apodemus sylvaticus
house elephant, mus domesticus
brown rat, rattus norvegicus

Any number of commands can be connected in this way (memory permitting), as long as each command takes text input and produces text output.

Even scripts or programs you write yourself.

You can chain any number of programs together to achieve your goal:

Linked processes illustration

This allows you to build up fairly complex workflows within one command-line.

Next activity: Reading - Redirection.