Jump to content

A Quick Introduction to Unix/Redirection

From Wikibooks, open books for an open world


Redirection

[edit | edit source]

When you issue a command that has some output, we say that by default it will write to the standard output which is the screen. If a command needs input we say it reads from the standard input which is the keyboard. The ls command which we have seen a lot of produces a list of files and directories as its output and it prints it on screen. We are going to use a new command - cat - to investigate how we can 'redirect' streams from the standard input or output.

Displaying file contents

[edit | edit source]

You can use the command cat to take input and write it to the standard output. Usually you use it like this

%cat myfile.txt

and it will put the contents of myfile.txt on screen and then return you to the prompt. It scrolls the contents very fast!

Using cat to capture from the keyboard

[edit | edit source]

When you use cat in the way described, the input is from a file and it writes to the standard output (you guessed it: the screen). But we can direct cat to take its input from elsewhere than a file and we can send its output to somewhere other than the screen.

You can use cat to capture what is typed at the keyboard and send it to some output. We can try it by typing cat without specifying a file name, like this

% cat

You can now type at the keyboard and when you have entered as many characters as you wish press return for a new line. When you are finished typing altogether, type Control-d (written as ^D for short) to end the process. If you run cat like this without naming a file to read, it reads the standard input (the keyboard) and on receiving the 'end of file' (^D), copies it to the standard output (the screen).

Redirecting the Output

[edit | edit source]

As I have said, you can redirect both the input and the output from commands.

You use the > symbol to redirect the output to a file. For example, to create a file called colours containing a list of colour names, type

% cat > colours

Then type in the names of some colours. Press [Return] after each one and end with ^D.

pink
yellow
purple
^D 

(this means press [Ctrl] and [d] to stop).

The cat command reads the standard input (the keyboard) and the > redirects the output (which would normally go to the screen) into a file called colours

To read the contents of the file, type

% cat colours

(This is not a particularly good way to create text files - normally I would recommend an editor like vi or emacs or pico but we're learning about readirection, not text editing.)

Appending to a file

[edit | edit source]

If you use >> to redirect, then standard output will be redirected to the end of the file and appended to the existing contents. So to add more items to the file colours, type

% cat >> colours

Then type in the names of some more colours

red
^D

(Control D to stop)

This redirects the keyboard input to the end of the file colours.

To read the contents of the file, type

% cat colours

Now we are going to create create a file colours2

% cat > colours2
green
blue
^D

(Control D to stop)

You now have two files. One lists four colours, the other two.

Joining two files together

[edit | edit source]

We will now use the cat command to join (concatenate) colours and colours2 into a new file called allcolours. This is in fact the original purpose of the command. You do it like this:

% cat colours colours2 > allcolours

What this is doing is reading the contents of colours and colours2 in turn and combining the results in the file allcolours. To read the contents of the new file, type

% cat allcolours

Redirecting the Input

[edit | edit source]

We use the < symbol to redirect the input for a command.

For example, the command sort sorts a list alphabetically or numerically. Type

% sort

Then type in the names of some animals. Press [Return] after each one.

dog
cat
bird
ape
^D

(control d to stop)

The output will be

ape
bird 
cat 
dog

Redirecting input to a file

[edit | edit source]

Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of colours, type

% sort < allcolours

and the sorted list will be output to the screen.

To redirect the output of the sorted list to a file, type,

% sort < allcolours > sortedcolours

Use cat to display the contents of the file sortedcolours.

Piping

[edit | edit source]

As well as redirecting the output of a command to a file, we can pass it along to another process using piping. For example

% sort < allcolours | wc -l

In this case, the output of the sort command is passed on to the wc command and processed before being displayed.