Hacking BSD, Part 1

Editor's note: BSD Hacks offers 100 practical tips, tricks, and tools for administrators and power users of BSD systems. To give you a taste of the kinds of "hacks" you'll find in the book, we've excerpted two here. Check back to this space next week for another sample hack on how to get the most out of FTP using macros and scripts.

Hack #13. Find Things

Finding fles in Unix can be an exercise in frustration for a novice user. Here's how to soften the learning curve.

Remember the first time you installed a Unix system? Once you successfully booted to a command prompt, I bet your first thought was, "Now what?" or possibly, "Okay, where is everything?" I'm also pretty sure your first foray into man find wasn't all that enlightening.

How can you as an administrator make it easier for your users to find things? First, introduce them to the built-in commands. Then, add a few tricks of your own to soften the learning curve.

Finding Program Paths

Every user should become aware of the three w's: which, whereis, and whatis. (Personally, I'd like to see some why and when commands, but that's another story.)

Use which to find the path to a program. Suppose you've just installed xmms and wonder where it went:

% which xmms
/usr/X11R6/bin/xmms

Better yet, if you were finding out the pathname because you wanted to use it in a file, save yourself a step:

% echo `which xmms` >> somefile

Remember to use the backticks (`), often found on the far left
of the keyboard on the same key as the tilde (~). If you instead
use the single quote (') character, usually located on the right side of the
keyboard on the same key as the double quote ("), your file will contain the
echoed string which xmms instead of the desired path.

The user's current shell will affect how which's switches work.
Here is an example from the C shell:

This is a matter of which which the user is using. Here, the user used the which which is built into the C shell and doesn't support the options used by the which utility. Where then is that which? Try the whereis command:

% whereis -b which
which: /usr/bin/which

Here, I used -b to search only for the binary. Without any switches, whereis will display the binary, the manpage path, and the path to the original sources.

If your users prefer to use the real which command instead of
the shell version and if they are only interested in seeing binary paths, consider
adding these lines to /usr/share/skel/dot.cshrc [Hack #9] :

alias which /usr/bin/which -a
alias whereis whereis -b

The -a switch will list all binaries with that name, not just the first binary found.

Finding Commands

How do you proceed when you know what it is that you want to do, but have no clue which commands are available to do it? I know I clung to the whatis command like a life preserver when I was first introduced to Unix. For example, when I needed to know how to set up PPP:

On the days I had time to satisfy my curiosity, I tried this variation:

% whatis "(1)"

That will show all of the commands that have a manpage in section 1. If you're rusty on your manpage sections, whatis intro should refresh your memory.

Finding Words

The previous commands are great for finding binaries and manpages, but what
if you want to find a particular word in one of your own text files? That requires
the notoriously user-unfriendly find command. Let's be realistic.
Even with all of your Unix experience, you still have to dig into either the
manpage or a good book whenever you need to find something. Can you really expect novice users to figure it out?

To start with, the regular old invocation of find will find filenames,
but not the words within those files. We need a judicious use of grep
to accomplish that. Fortunately, find's -exec switch
allows it to use other utilities, such as grep, without forking
another process.

Start off with a find command that looks like this:

% find . -type f -exec grep "word" { } \;

This invocation says to start in the current directory (.), look through files,
not directories (-type f), while running the grep
command (-exec grep) in order to search for the word word.
Note that the syntax of the -exec switch always resembles:

-exec command with_its_parameters { } \;

What happens if I search the files in my home directory for the word alias?

While it's nice to see that find successfully found the word alias
in my home directory, there's one slight problem. I have no idea which
file or files contained my search expression! However, adding /dev/null
to that command will fix that:

Why did adding nothing, /dev/null, automagically cause the name of
the file to appear next to the line that contains the search expression? Is
it because Unix is truly amazing? After all, it does allow even the state of
nothingness to be expressed as a filename.

Actually, it works because grep will list the filename whenever
it searches multiple files. When you just use { }, find
will pass each filename it finds one at a time to grep. Since grep
is searching only one filename, it assumes you already know the name of that
file. When you use /dev/null { }, find actually passes
grep two files, /dev/null along with whichever file find
happens to be working on. Since grep is now comparing two files,
it's nice enough to tell you which of the files contained the search string.
We already know /dev/null won't contain anything, so we just convinced
grep to give us the name of the other file.

That's pretty handy. Now let's make it friendly. Here's a very simple script
called fstring:

That $1 is a positional parameter. This script expects the user
to give one parameter: the word the user is searching for. When the script executes,
the shell will replace "$1" with the user's search string. So,
the script is meant to be run like this:

% fstring word_to_search

If you're planning on using this script yourself, you'll probably remember
to include a search string. If you want other users to benefit from the script,
you may want to include an if statement to generate an error message
if the user forgets the search string:

Don't forget to make your script executable with chmod +x and
to place it in the user's path. /usr/local/bin is a good location for
other users to benefit.

See Also

man which

man whereis

man whatis

man find

man grep

Hack #21. Manage Temporary Files and Swap Space

Add more temporary or swap space without repartitioning.

When you install any operating system, it's important to allocate sufficient
disk space to hold temporary and swap files. Ideally, you already know the optimum
sizes for your system so you can partition your disk accordingly during the
install. However, if your needs change or you wish to optimize your initial
choices, your solution doesn't have to be as drastic as a repartition - and
reinstall - of the system.

TIP: man tuning has some practical advice for guesstimating
the appropriate size of swap and your other partitions.

Clearing /tmp

Unless you specifically chose otherwise when you partitioned your disk, the
installer created a /tmp filesystem for you:

Here I searched /etc/fstab for the /tmp filesystem. This
particular filesystem is 256 MB in size. Only a small portion contains temporary
files.

TIP: The df (disk free) command will always show you
a number lower than the actual partition size. This is because eight percent
of the filesystem is reserved to prevent users from inadvertently overflowing
a filesystem. See man tunefs for details.

It's always a good idea to clean out /tmp periodically so it doesn't
overflow with temporary files. Consider taking advantage of the built-in periodic
script /etc/periodic/daily/110.clean-tmps [Hack #20] .

You can also clean out /tmp when the system reboots by adding this
line to /etc/rc.conf:

clear_tmp_enable="YES"

Moving /tmp to RAM

Another option is to move /tmp off of your hard disk and into RAM.
This has the built-in advantage of automatically clearing the filesystem when
you reboot, since the contents of RAM are volatile. It also offers a performance
boost, since RAM access time is much faster than disk access time.

Before moving /tmp, ensure you have enough RAM to support your desired
/tmp size. This command will show the amount of installed RAM:

Notice that the filesystem is now md0, the first memory disk,
instead of ad0s1e, a partition on the first IDE hard drive.

Creating a Swap File on Disk

Swap is different than /tmp. It's not a storage area for temporary
files; instead, it is an area where the filesystem swaps data between
RAM and disk. A sufficient swap size can greatly increase the performance of
your filesystem. Also, if your system contains multiple drives, this swapping
process will be much more efficient if each drive has its own swap partition.

Note that the swapinfo command displays the size of your swap
files. If you prefer to see that output in MB, try the swapctl
command with the -lh flags (which make the listing more
human):

% swapctl -lh
Device: 1048576-blocks Used:
/dev/ad0s1b 624 0

To add a swap area, first determine which area of disk space to use. For example,
you may want to place a 128 MB swapfile on /usr. You'll first need
to use dd to create this as a file full of null (or zero) bytes.
Here I'll create a 128 MB swapfile as /usr/swap0:

Next, change the permissions on this file. Remember, you don't want users storing
data here; this file is for the filesystem:

# chmod 600 /usr/swap0

Since this is really a file on an existing filesystem, you can't mount
your swapfile in /etc/fstab. However, you can tell the system to find
it at boot time by adding this line to /etc/rc.conf:

swapfile="/usr/swap0"

To start using the swapfile now without having to reboot the system, use mdconfig:

# mdconfig -a -t vnode -f /usr/swap0 -u 1 && swapon /dev/md1

The -a flag attaches the memory disk. -t vnode marks
that the type of swap is a file, not a filesystem. The -f flag
sets the name of that file: /usr/swap0.

The unit number -u 1 must match the name of the memory disk /dev/md1.
Since this system already has /tmp mounted on /dev/md0, I
chose to mount swap on /dev/md1. && swapon tells
the system to enable that swap device, but only if the mdconfig
command succeeded.

See Also

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.