Redirection and combining commands are relatively simple in Linux, and
fabulously enhance its flexibility. These would very aptly be called
the `little things' in Linux, because these are very compact
expressions which are however indispensible, like the the little
things such as `politeness' in life. I'll just list them here for
reference:
The simplest way to `combine' commands is to use the wildcard, *. This allows you to apply the command to
files of similar types.
For example
% grep start *.dat
searches for the string start in all data files.
Exercise 13: Look again at the exercise with grep using the word `planet'. Use the wildcard, *, to find the word
`planet' in all .txt files in the directory docs.
If you would like to send output or take input from nonstandard
directions, like for instance storing which output in a file,
use one of the redirectors, ,
or
, which
get input from a file, send output to a file, or append to a file,
respectively.
An example is the cat
file command we've already seen,
which redirects input from the keyboard into a file. Likewise,
cat
redirects input from keyboard to the end of an existing file.
Another way to combine commands uses the semicolon `;', allowing you to combine several commands on one line.
For example, from the directory tutorial,
% cd texts;pwd;ls -al
changes directories to texts, checks the directory name, and lists all
the files in it.
Finally, inserting a `' between commands is yet another way to
combine them. The syntax command1
command2 means send the
output of command1 into the input of command2.
For example:
% grep turn test.txt
And returned on the previous night.
%,
which gives the line containing the string turn, can be reproduced
in the following way:
% cat test.txt | grep turn
And returned on the previous night.
% .
In the second case, we've used two commands instead of one. The input to grep was
the entire file test.txt. grep then proceeded to search
for the string turn in that input. In this example, you see that
`' served very much like a `pipeline' between two commands! This
pipeline is what makes `
' different from `;', in that the pipe
allows the commands to share information. On the other hand, `;'
merely separates the commands: there is no information passed through
the `;'.