Redirection and Manipulation

Help Center Introduction to Unix Redirection and Manipulation

Redirection

The > operator redirects output to a file. It takes whatever information would normally go to the screen (or elsewhere), and puts it in a file that you name, instead. Let's look at an example of > usage by redirecting the output of the who command to a file.

% who
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
% who > currentusers
% ls
currentusers otherfile otherfile.old otherfile2 otherfile3
% cat currentusers
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)

The >> operator works the same as the > operator, except that it appends information to a file instead of overwriting it. Used with the cat concatenate command, you can append the contents of one file to another. Used with the echo command, you can append text to a file.

% cp currentusers currentuserstwice
% cat currentusers >> currentuserstwice
% cat currentuserstwice
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
% echo "this is the end" >> currentuserstwice
% cat currentuserstwice
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
this is the end

Piping

In addition to redirecting input/output to a named file, you can connect two commands together so that the output from one command becomes the input of another command. This is called "piping" a file to another, because the pipe (|) character is used. When a | operator is set between two (or more, for that matter) commands, the standard output of the command to the left of the pipe becomes the standard input of the command to the right of the pipe. Any two commands can be used together in this manner as long as the first command writes to standard output and the second command reads from standard input.

Here's an example of piping output from one program to another, using the who command. We want to take the output from who, and send it to the sort program.

% who
nhart pts/0 Jul 10 11:54 (216.254.9.27)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
avedis pts/3 Sep 12 15:08 (135.214.11.57)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)
eliot pts/8 Sep 11 16:18 (homer02)
% who | sort
avedis pts/3 Sep 12 15:08 (135.214.11.57)
dbluft pts/1 Sep 6 11:52 (140.142.14.39)
dbluft pts/2 Sep 12 13:54 (140.142.14.39)
eliot pts/8 Sep 11 16:18 (homer02)
mstark pts/4 Sep 12 15:46 (homer37.u.washin)
nhart pts/0 Jul 10 11:54 (216.254.9.27)
nhart pts/5 Sep 10 02:56 (216.254.9.27)
nhart pts/6 Jul 3 08:56 (216.254.9.27)

In our humble opinions, the most useful thing to pipe output to is the grep command. grep is a program that looks for a pattern within a file or command line output.

% ls -l
total 4
-rw-r--r-- 1 mstark user 458 Sep 12 15:47 currentusers
-rw-r--r-- 1 mstark user 932 Sep 12 15:55 currentuserstwice
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
-rw-r--r-- 1 mstark user 0 Sep 12 15:21 otherfile.old
-rw-r--r-- 1 mstark user 0 Sep 12 15:20 otherfile2
-rw-r--r-- 1 mstark user 0 Sep 12 15:21 otherfile3
% ls -l | grep other
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
-rw-r--r-- 1 mstark user 0 Sep 12 15:21 otherfile.old
-rw-r--r-- 1 mstark user 0 Sep 12 15:20 otherfile2
-rw-r--r-- 1 mstark user 0 Sep 12 15:21 otherfile3
% ls -l | grep other | grep old
-rw-r--r-- 1 mstark user 0 Sep 12 15:21 otherfile.old