Let's pretend you've posted a question to the FreeBSD questions list, and
someone has asked you to supply the output of uname -a, dmesg, and
fstab. You could use > to create three files and paste them into your
e-mail document. But you could also redirect all three outputs to one file
by using the >> redirector instead:

I only used one > in the uname -a command because I was creating a new file and
wasn't concerned with overwriting its contents. The command would have
worked with >> but I saved myself a keystroke.

Because I used >> in the dmesg command, I didn't overwrite the uname -a
portion of the file called help.

Note that I needed to use the more command with /etc/fstab. If I type:

fstab >> /usr/home/genisis/help,

fstab: Command not found:

will be the resultant error message. You can't redirect files; you can
only redirect the output of commands. In this case, more is the command
that read /etc/fstab; the result of that reading could then be redirected
to /usr/home/genisis/help.

So you've successfully sent three commands to one file without overwriting
each other's output. But why type in three sets of commands? Surely there
must be a way to do this with one command. If I type:

uname -a dmesg more /etc/fstab >> /usr/home/genisis/help2,

usage: uname [-amnrsv]

will be the message I'll receive back. Take a look at that command
again; it has everything you want to do, but how can you tell what is a
command, what is a switch, and what is a file? If you're confused looking
at your own command, imagine how your shell feels when it's trying to
interpret what it is you want to do. We need some way of separating our
commands; try:

uname -a; dmesg; more /etc/fstab >> /usr/home/genisis/help3

This should yield the output of uname -a and dmesg to your terminal; if you

more /usr/home/genisis/help3

you will only see the output of more /etc/fstab

So, we're getting closer. We've separated the commands, now we want the
shell to know that we want all three outputs in the file, not just the
last command's output. One last try:

(uname -a; dmesg; more /etc/fstab) >> /usr/home/genisis/help4

more /usr/home/genisis/help4

and you should have the results you wanted to achieve. The parentheses tell
your shell that you want to run the commands in the parentheses first, then
redirect all of that output to your file.

FreeBSD comes with several handy utilities for finding information. Which
utility you use depends on what it is you're trying to find. If you need
to find an application, use whereis:

whereis ls

ls: /bin/ls

If you need to find a file, use locate:

locate fstab

/etc/fstab

If you find something but don't know what it is, use whatis:

whatis ls

ls(1) - list directory contents

whatis fstab

fstab(5) - static information about the filesystems

Note that whatis will include a manpage number in brackets. If you want
additional information on the above, you can type

man 1 ls

man 5 fstab

But what if you need to find a specific piece of text? You'll need to use
the grep utility, which has a very simple syntax:

grep whatyou'relookingfor filename

Let's suppose I can't remember if the bpf device is enabled or
disabled in the default FreeBSD 4.0 kernel. I could open up this file and
start reading it, but it's quicker to ask:

Of course, I can redirect this output to a file to e-mail to the person who
asked me in the first place:

grep -n bpf /usr/src/sys/i386/conf/GENERIC > /usr/home/genisis/reply

As another example, if you want the CPU information for your computer, you
could run dmesg, turn on your scroll lock button, page up, and look for
the line that describes your CPU. Or you could:

dmesg | grep CPU

CPU: AMD-K6tm w/ multimedia extensions
(199.96-MHz 586-class CPU)

to quickly receive the same information. Note, if you try the command this
way:

grep CPU dmesg

grep: dmesg: No such file or directory)

will be the error message. Dmesg is a command and you can only grep
files. However, you can pipe the output of a command to grep so it can
find a certain piece of text.

To summarize: when you use grep, ask yourself if what you're looking for
will be found in a file or if it is the result of a command. If it's in a
file, use:

grep text filename

If it's the result of a command, use:

command | grep text

If you're interested in learning other essential commands, there is an
excellent online beginners tutorial, Introduction to Unix.

Next week we'll take a break from learning commands and introduce using a
network monitoring utility.

Dru Lavigne
is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.