Piping and Redirection
an Illustration of two UNIXconcepts
What are redirection and piping?
UNIX commands and files can be used in conjunction with oneanother to turn simple commands into much more
complex co mmands. When these conjunctions involve files, wecall it redirection. When these conjunction involve
commands we call it piping.
The following are the various forms thatredirection and piping can have: p>
(that is, how they'll look on your commandline).
| (pipe)
< (redirect input from a file)
> (redirect output to a file)
>> (redirect output and append to a file)
tee (redirect output to a file and pipe)
Let’s explore each of the above more fullyand see some examples of each.
| (pipe):
The "|" symbol is used to indicate that the outputfrom one command will be used as t he input for another command.
The parameters on both sides must have outputs and be able toaccepts inputs, respectively for this command to work
properly. Let’s look at an example of this:
The simplest example of piping can be ill ustrated by using adirectory command (ls). I’m sure many of you have done a
directory listing and have screens of text fly by your faceuntil you discovered the option which allows you to view the
directory page by page. In UNIX this is actually a combinationof two commands. The first is the directory and the
second is the command which takes the output and formats it tobe viewed on screen at a time, the "more" command.
The completed command looks like this:
ls | more
< (redirect input from a file)
This option allows you to take any file and use its contentsas the input for a command. Let’s say you wanted to mail a
document to someone. To do this in UNIX you could mail it asan atta chment or you could make it the body of the
message you were sending in the following way:
mail userx < somefile.txt
where mail is the command to send mail to a user, userx is theuser you are sending it to and somefile.txt is the fil e you
want to send.
> (redirect output to a file)
This option will take the output of a command and send it to afile. If you wanted to store the output of a list operation
(ls) in a file you would do the following:
ls &g t; somefile.txt
where "ls" is the directory operation, andsomefile.txt is the file in which you want to store theinformation.
>> (redirect output and append to a file)
This option is very similar to the last except it add s the newinput for the file at the end of the file. This would be usefulif
you had a series of outputs for some function and you wantedto save all them.
tee (redirect output to a file and pipe)
The point of this is to save the output of a command to a fileand at the same time use that output as the standard input
for a another command. The command would take this form:
ls |tee test.txt|more
where ls is the list command test.txt is the file to which youwant to save the output and more is the formatting
command to view a directory page by page.
Activities:
Now that we’ve learned about redirection and piping tryto answer these questions. Note: if there are an y parts of thecommand that you don’t understand go to your UNIX shell andtype "man <command>"
where <command> is the command you don’tunderstand.
Here’s another excise for you to try:
1. Try this command:
banner fun
2. Now, try "redirecting& quot; the results ofthat command to a file:
banner fun > shkoo
3. Look at the contents of the file:
cat shkoo
4. Add another word to the shkoo file (notice thedouble >>):
ba nner yay >> shkoo
cat shkoo
5. Finally, let's pipe the output of the bannercommand to another command, mail:
banner whee | mail <your login>
CAUTION: replace <your login> with your login name. So, if your login
was jsmith, the command would be: banner whee | mailjsmith
5a. Now, go check your email.
The banner command only has a standard output, so we can'tuse the <
and make it get its input from a file.
6. Sort does use a standard input. So, although it's atad silly, try:
sort < shkoo