FreeBSD comes with a lot of simple yet powerful commands designed to make your computing work easier. No matter how long you've used any type of Unix system, you'll still discover new shortcuts and new ways of doing things more efficiently. As my grandmother used to say, there's more than one way to skin a cat.
In this article, we'll discuss commands that help you remember who you are, where you are, how to find things, and how to start thinking like a Unix geek when it comes to accomplishing tasks.
I love the concept of virtual terminals and usually run all 8 along with an XWindows session. At a typical moment, I'll have a PPP session running, another terminal with an e-mail client open, a couple of terminals with different man pages open, another terminal where I'm trying out commands as root, another terminal where I'm trying out commands as a regular user -- you get the idea. With this increased functionality comes increased confusion. I use several commands to help me navigate this mess.
If I forget which terminal I left a man page at, the PrintScrn key will scroll through all virtual terminals in increasing order. If I'm not running an X Window session, I can continue to rotate through terminals 1 to 8 forever. If I am running an X Window session, it will stop at terminal 9, the X Server.
If I enter a terminal and wish to know which one it is, I use the
Note that this is actually virtual terminal 5 as they are numbered starting from 0. If I leave this terminal, Alt-F5 will get me back there.
If I want to know who I am in this terminal, I can use
rootIf I want to know who is logged into any terminal, I can use
genisis ttyv0 Jun 3 15:45 genisis ttyv1 Jun 3 15:46 genisis ttyv2 Jun 3 21:09 genisis ttyv3 Jun 3 21:10 genisis ttyv4 Jun 3 21:27 genisis ttyv5 Jun 4 09:40 genisis ttyv6 Jun 4 09:43 genisis ttyv7 Jun 4 10:46
Note the difference between
whoami. On ttyv4, I originally
logged in as genisis, then became superuser. The
who command will tell you
who has the login shell but does not return information on non-login
shells. Also, because my X Window session is not a login shell, ttyv8 does
not display in this output.
If I forget where I am in the directory structure, I use
which will show my present working directory:
Good rule of thumb: Never make or
delete files or directories without first using
pwd to double-check that
you really are where you want to be.
Now, if I've lost all track of time:
Sun Jun 4 11:15:46 EDT 2000
or worse, can't remember what day it is:
June 2000 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
or need to know what day Christmas falls on in the year 2020:
cal 12 2020
December 2020 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
or what day of the week the Declaration of Independence was signed:
cal 07 1776
July 1776 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
For two last geek trivia bits on
cal to amaze your friends and confound your enemies; try:
cal 9 1752
September 1752 Su Mo Tu We Th Fr Sa 1 2 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
The missing dates are due to the switch from the Julian to the Gregorian calendars. And:
April 23 2000
will show which date Easter falls on; use
-o if you want to know the date for Orthodox Easter.
By now, you're probably convinced that I'm a wonderful typist who has so carefully typed into this article the calendars you are viewing. Let's take a look at my history list:
110 date > /usr/home/genisis/cal 111 cal > /usr/home/genisis/cal 112 cal 12 2020 > /usr/home/genisis/cal 113 cal 07 1776 > /usr/home/genisis/cal 117 cal 9 1752 > /usr/home/genisis/cal 122 ncal -e > /usr/home/genisis/cal 134 h > /usr/home/genisis/cal
I hate to type and use the
> redirector a lot. This redirector is used if you want to save the results of a command to a file; the syntax is always
command > filename
I wanted you to see the output for each of the commands in the above
history list, so I redirected them to a file, then pasted that file into
my document. Note that I overwrote the same file seven times; this
happened because I only used one
Let's pretend you've posted a question to the FreeBSD questions list, and
someone has asked you to supply the output of
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:
uname -a > /usr/home/genisis/help dmesg >> /usr/home/genisis/help more /etc/fstab >> /usr/home/genisis/help
If I now enter:
I'll see the output of
dmesg, and my
Let's look at these commands more carefully:
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
>> but I saved myself a keystroke.
Because I used
>> in the
dmesg command, I didn't overwrite the
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
/etc/fstab; the result of that reading could then be redirected
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
you will only see the output of
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
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
If you need to find a file, use locate:
If you find something but don't know what it is, use
ls(1) - list directory contents
fstab(5) - static information about the filesystems
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
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:
grep bpf /usr/src/sys/i386/conf/GENERIC
# The `bpf' pseudo-device enables the Berkeley Packet Filter. pseudo-device bpf #Berkeley packet filter
If I couldn't remember how far into this file the bpf option was, I could add a switch to grep:
grep -n bpf /usr/src/sys/i386/conf/GENERIC
212:# The `bpf' pseudo-device enables the Berkeley Packet Filter. 214:pseudo-device bpf #Berkeley packet filter
will list the line numbers of the entries.
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.
Read more FreeBSD Basics columns.
Discuss this article in the Operating Systems Forum.
Return to the BSD DevCenter.
Copyright © 2009 O'Reilly Media, Inc.