Now that we know a few basic commands, we can finally look at the shell's most powerful feature: the ease with which it lets us combine existing programs in new ways. We'll start with a directory called molecules that contains six files describing some simple organic molecules. The .pdb extension indicates that these files are in Protein Data Bank format, a simple text format that specifies the type and position of each atom in the molecule.
%%bash2 --dir ~/library/data/data-shell
ls molecules
Let's go into that directory with cd and run the command wc *.pdb
. wc
is the "word count" command: it counts the number of lines, words, and characters in files (from left to right, in that order).
The *
in *.pdb
matches zero or more characters, so the shell turns *.pdb
into a list of all .pdb
files in the current directory:
%%bash2
cd molecules
wc *.pdb
If we run wc -l
instead of just wc
, the output shows only the number of lines per file:
%%bash2
wc -l *.pdb
We can also use -w
to get only the number of words, or -c
to get only the number of characters.
Which of these files is shortest? It's an easy question to answer when there are only six files, but what if there were 6000? Our first step toward a solution is to run the command:
%%bash2
wc -l *.pdb > lengths.txt
The greater than symbol, >
, tells the shell to redirect the command's output to a file instead of printing it to the screen. (This is why there is no screen output: everything that wc would have printed has gone into the file lengths.txt
instead.) The shell will create the file if it doesn't exist. If the file exists, it will be silently overwritten, which may lead to data loss and thus requires some caution. ls lengths.txt
confirms that the file exists:
%%bash2
ls lengths.txt
We can now send the content of lengths.txt
to the screen using cat lengths.txt
. cat stands for "concatenate": it prints the contents of files one after another. There's only one file in this case, so cat just shows us what it contains:
%%bash2
cat lengths.txt
Now let's use the sort
command to sort its contents.
We will also use the -n
flag to specify that the sort is numerical instead of alphabetical. This does not change the file; instead, it sends the sorted result to the screen:
%%bash2
sort -n lengths.txt
We can put the sorted list of lines in another temporary file called sorted-lengths.txt
by putting > sorted-lengths.txt
after the command, just as we used > lengths.txt
to put the output of wc
into lengths.txt
. Once we've done that, we can run another command called head
to get the first few lines in sorted-lengths.txt
:
%%bash2
sort -n lengths.txt > sorted-lengths.txt
head -n 1 sorted-lengths.txt
Using -n 1
with head
tells it that we only want the first line of the file; -n 20
would get the first 20, and so on. Since sorted-lengths.txt
contains the lengths of our files ordered from least to greatest, the output of head must be the file with the fewest lines.
If you think this is confusing, you're in good company: even once you understand what wc
, sort
, and head
do, all those intermediate files make it hard to follow what's going on. We can make it easier to understand by running sort
and head
together:
%%bash2
sort -n lengths.txt | head -n 1
The vertical bar, |
, between the two commands is called a pipe. It tells the shell that we want to use the output of the command on the left as the input to the command on the right. The computer might create a temporary file if it needs to, or copy data from one program to the other in memory, or something else entirely; we don't have to know or care.
Nothing prevents us from chaining pipes consecutively. That is, we can for example send the output of wc
directly to sort
, and then the resulting output to head
. Thus we first use a pipe to send the output of wc
to sort
:
%%bash2
wc -l *.pdb | sort -n
And now we send the output of this pipe, through another pipe, to head
, so that the full pipeline becomes:
%%bash2
wc -l *.pdb | sort -n | head -n 1
This is exactly like a mathematician nesting functions like log(3x) and saying "the log of three times x". In our case, the calculation is "head of sort of line count of *.pdb
".
Here's what actually happens behind the scenes when we create a pipe. When a computer runs a program - any program - it creates a process in memory to hold the program's software and its current state. Every process has an input channel called standard input. (By this point, you may be surprised that the name is so memorable, but don't worry: most Unix programmers call it "stdin"). Every process also has a default output channel called standard output (or "stdout"). A second output channel called standard error (stderr) also exists. This channel is typically used for error or diagnostic messages, and it allows a user to pipe the output of one program into another while still receiving error messages in the terminal.
The shell is actually just another program. Under normal circumstances, whatever we type on the keyboard is sent to the shell on its standard input, and whatever it produces on standard output is displayed on our screen. When we tell the shell to run a program, it creates a new process and temporarily sends whatever we type on our keyboard to that process's standard input, and whatever the process sends to standard output to the screen.
Here's what happens when we run wc -l *.pdb > lengths.txt
. The shell starts by telling the computer to create a new process to run the wc
program. Since we've provided some filenames as arguments, wc
reads from them instead of from standard input. And since we've used >
to redirect output to a file, the shell connects the process's standard output to that file.
If we run wc -l *.pdb | sort -n
instead, the shell creates two processes (one for each process in the pipe) so that wc
and sort
run simultaneously. The standard output of wc
is fed directly to the standard input of sort; since there's no redirection with >
, sort
's output goes to the screen. And if we run wc -l *.pdb | sort -n | head -n 1
, we get three processes with data flowing from the files, through wc
to sort
, and from sort
through head
to the screen.
This simple idea is why Unix has been so successful. Instead of creating enormous programs that try to do many different things, Unix programmers focus on creating lots of simple tools that each do one job well, and that work well with each other. This programming model is called "pipes and filters". We've already seen pipes; a filter is a program like wc
or sort
that transforms a stream of input into a stream of output. Almost all of the standard Unix tools can work this way: unless told to do otherwise, they read from standard input, do something with what they've read, and write to standard output.
The key is that any program that reads lines of text from standard input and writes lines of text to standard output can be combined with every other program that behaves this way as well. You can and should write your programs this way so that you and other people can put those programs into pipes to multiply their power.
Nelle has run her samples through the assay machines and created 17 files in the north-pacific-gyre/2012-07-03
directory described earlier. As a quick sanity check, starting from her home directory, Nelle types:
cd north-pacific-gyre/2012-07-03
wc -l *.txt
The output is 18 lines that look like this:
300 NENE01729A.txt
300 NENE01729B.txt
300 NENE01736A.txt
300 NENE01751A.txt
300 NENE01751B.txt
300 NENE01812A.txt
... ...
Now she types this:
wc -l *.txt | sort -n | head -n 5
240 NENE02018B.txt
300 NENE01729A.txt
300 NENE01729B.txt
300 NENE01736A.txt
300 NENE01751A.txt
Whoops: one of the files is 60 lines shorter than the others. When she goes back and checks it, she sees that she did that assay at 8:00 on a Monday morning — someone was probably in using the machine on the weekend, and she forgot to reset it. Before re-running that sample, she checks to see if any files have too much data:
wc -l *.txt | sort -n | tail -n 5
300 NENE02040B.txt
300 NENE02040Z.txt
300 NENE02043A.txt
300 NENE02043B.txt
5040 total
Sure enough, when she checks the log on her laptop, there's no depth recorded for either of those samples. Since it's too late to get the information any other way, she must exclude those two files from her analysis. She could just delete them using rm
, but there are actually some analyses she might do later where depth doesn't matter, so instead, she'll just be careful later on to select files using the wildcard expression *[AB].txt
. As always, the *
matches any number of characters; the expression [AB]
matches either an 'A' or a 'B', so this matches all the valid data files she has.