BUY THIS BOOK
Add to Cart

Print Book $24.95


Add to Cart

Print+PDF $32.44

Add to Cart

PDF $19.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £17.50

What is this?

Looking to Reprint or License this content?


BSD Hacks
BSD Hacks 100 Industrial Tip & Tools By Dru Lavigne
May 2004
Pages: 448

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Customizing the User Environment
Users of open source (http://opensource.org) Unix operating systems are an interesting breed. They like to poke under the surface of things, to find out how things work, and to figure out new and interesting ways of accomplishing common computing tasks. In short, they like to "hack."
While this book concentrates on the BSDs, many of the hacks apply to any open source operating system. Each hack is simply a demonstration of how to examine a common problem from a slightly different angle. Feel free to use any of these hacks as a springboard to your own customized solution. If your particular operating system doesn't contain the tool used in the solution, use a tool that does exist, or invent your own!
This chapter provides many tools for getting the most out of your working environment. You'll learn how to make friends with your shell and how to perform your most common tasks with just a few keystrokes or mouse clicks. You'll also uncover tricks that can help prevent command-line disasters. And, above all, you'll discover that hacking BSD is fun. So, pull your chair up to your operating system of choice and let's start hacking.
Become a speed daemon at the command line.
For better or for worse, you spend a lot of time at the command line. If you're used to administering a Linux system, you may be dismayed to learn that bash is not the default shell on a BSD system, for either the superuser or regular user accounts.
Take heart; the FreeBSD superuser's default tcsh shell is also brimming with shortcuts and little tricks designed to let you breeze through even the most tedious of tasks. Spend a few moments learning these tricks and you'll feel right at home. If you're new to the command line or consider yourself a terrible typist, read on. Unix might be a whole lot easier than you think.
NetBSD and OpenBSD also ship with the C shell as their default shell. However, it is not always the same
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
Users of open source (http://opensource.org) Unix operating systems are an interesting breed. They like to poke under the surface of things, to find out how things work, and to figure out new and interesting ways of accomplishing common computing tasks. In short, they like to "hack."
While this book concentrates on the BSDs, many of the hacks apply to any open source operating system. Each hack is simply a demonstration of how to examine a common problem from a slightly different angle. Feel free to use any of these hacks as a springboard to your own customized solution. If your particular operating system doesn't contain the tool used in the solution, use a tool that does exist, or invent your own!
This chapter provides many tools for getting the most out of your working environment. You'll learn how to make friends with your shell and how to perform your most common tasks with just a few keystrokes or mouse clicks. You'll also uncover tricks that can help prevent command-line disasters. And, above all, you'll discover that hacking BSD is fun. So, pull your chair up to your operating system of choice and let's start hacking.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Get the Most Out of the Default Shell
Become a speed daemon at the command line.
For better or for worse, you spend a lot of time at the command line. If you're used to administering a Linux system, you may be dismayed to learn that bash is not the default shell on a BSD system, for either the superuser or regular user accounts.
Take heart; the FreeBSD superuser's default tcsh shell is also brimming with shortcuts and little tricks designed to let you breeze through even the most tedious of tasks. Spend a few moments learning these tricks and you'll feel right at home. If you're new to the command line or consider yourself a terrible typist, read on. Unix might be a whole lot easier than you think.
NetBSD and OpenBSD also ship with the C shell as their default shell. However, it is not always the same tcsh, but often its simpler variant, csh, which doesn't support all of the tricks provided in this hack.
However, both NetBSD and OpenBSD provide a tcsh package in their respective package collections.
I hate to live without three keys: up arrow, down arrow, and Tab. In fact, you can recognize me in a crowd, as I'm the one muttering loudly to myself if I'm on a system that doesn't treat these keys the way I expect to use them.
tcsh uses the up and down arrow keys to scroll through your command history. If there is a golden rule to computing, it should be: "You should never have to type a command more than once." When you need to repeat a command, simply press your up arrow until you find the desired command. Then, press Enter and think of all the keystrokes you just saved yourself. If your fingers fly faster than your eyes can read and you whiz past the right command, simply use the down arrow to go in the other direction.
The Tab key was specifically designed for both the lazy typist and the terrible speller. It can be painful watching some people type out a long command only to have it fail because of a typo. It's even worse if they haven't heard about history, as they think their only choice is to try typing out the whole thing all over again. No wonder some people hate the command line!
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Useful tcsh Shell Configuration File Options
Make the shell a friendly place to work in.
Now that you've had a chance to make friends with the shell, let's use its configuration file to create an environment you'll enjoy working in. Your prompt is an excellent place to start.
The default tcsh prompt displays % when you're logged in as a regular user and hostname# when you're logged in as the superuser. That's a fairly useful way to figure out who you're logged in as, but we can do much better than that.
Each user on the system, including the superuser, has a .cshrc file in his home directory. Here are my current prompt settings:
dru@~:grep prompt ~/.cshrc
if ($?prompt) then
        set prompt = "%B%n@%~%b: "
That isn't the default tcsh prompt, as I've been using my favorite customized prompt for the past few years. The possible prompt formatting sequences are easy to understand if you have a list of possibilities in front of you. That list is buried deeply within man cshrc, so here's a quick way to zero in on it:
dru@~:man cshrc
               /prompt may include
            
Here I've used the / to invoke the manpage search utility. The search string prompt may include brings you to the right section, and is intuitive enough that even my rusty old brain can remember it.
If you compare the formatting sequences shown in the manpage to my prompt string, it reads as follows:
set prompt = "%B%n@%~%b: "
That's a little dense. Table 1-1 dissects the options.
Table 1-1: Prompt characters
Character
Explanation
"
Starts the prompt string.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Create Shell Bindings
Train your shell to run a command for you whenever you press a mapped key.
Have you ever listened to a Windows power user expound on the joys of hotkeys? Perhaps you yourself have been known to gaze wistfully at the extra buttons found on a Microsoft keyboard. Did you know that it's easy to configure your keyboard to launch your most commonly used applications with a keystroke or two?
One way to do this is with the bindkey command, which is built into the tcsh shell. As the name suggests, this command binds certain actions to certain keys. To see your current mappings, simply type bindkey. The output is several pages long, so I've included only a short sample. However, you'll recognize some of these shortcuts from [Hack #1] .
Standard key bindings
"^A"           ->  beginning-of-line
"^B"           ->  backward-char
"^E"           ->  end-of-line
"^F"           ->  forward-char
"^L"           ->  clear-screen
"^N"           ->  down-history
"^P"           ->  up-history
"^U"           ->  kill-whole-line

Arrow key bindings
down           -> history-search-forward
up             -> history-search-backward
left           -> backward-char
right          -> forward-char
home           -> beginning-of-line
end            -> end-of-line
The ^ means hold down your Ctrl key. For example, press Ctrl and then l, and you'll clear your screen more quickly than by typing clear. Notice that it doesn't matter if you use the uppercase or lowercase letter.
One of my favorite shortcuts isn't bound to a key by default: complete-word-fwd. Before I do the actual binding, I'll first check which keys are available:
dru@~:bindkey | grep undefined

"^G"           ->  is undefined
"\305"         ->  is undefined
"\307"         ->  is undefined
<snip>
Although it is possible to bind keys to numerical escape sequences, I don't find that very convenient. However, I can very easily use that available Ctrl-g. Let's see what happens when I bind it:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Use Terminal and X Bindings
Take advantage of your terminal's capabilities.
It's not just the tcsh shell that is capable of understanding bindings. Your FreeBSD terminal provides the kbdcontrol command to map commands to your keyboard. Unfortunately, neither NetBSD nor OpenBSD offer this feature. You can, however, remap your keyboard under X, as described later.
Let's start by experimenting with some temporary mappings. The syntax for mapping a command with kbdcontrol is as follows:
kbdcontrol -f number "command"
Table 1-2 lists the possible numbers, each with its associated key combination.
Table 1-2: Key numbers
Number
Key combination
1, 2, . . . 12
F1, F2, . . . F12
13, 14, . . . 24
Shift+F1, Shift+F2, . . . Shift+F12
25, 26, . . . 36
Ctrl+F1, Ctrl+F2, . . . Ctrl+F12
37, 38, . . . 48
Shift+Ctrl+F1, Shift+Ctrl+F2, . . . Shift+Ctrl+F12
49
Home
50
Up arrow
51
Page Up
52
Numpad - (Num Lock off)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Use the Mouse at a Terminal
Use your mouse to copy and paste at a terminal.
If you're used to a GUI environment, you might feel a bit out of your element while working at the terminal. Sure, you can learn to map hotkeys and to use navigational tricks, but darn it all, sometimes it's just nice to be able to copy and paste!
Don't fret; your mouse doesn't have to go to waste. In fact, depending upon how you have configured your system, the mouse daemon moused may already be enabled. The job of this daemon is to listen for mouse data in order to pass it to your console driver.
Of course, if you're using screen [Hack #12] , you can also take advantage of its copy and paste mechanism.
If you installed and configured X when you installed your system, moused is most likely started for you when you boot up. You can check with this:
% grep moused /etc/rc.conf
moused_port="/dev/psm0"
moused_type="auto"
moused_enable="YES"
Very good. moused needs to know three things:
  • The mouse port (in this example, /dev/psm0, the PS/2 port)
  • The type of protocol (in this example, auto)
  • Whether to start at boot time
If you receive similar output, you're ready to copy and paste.
To copy text, simply select it by clicking the left mouse button and dragging. Then, place the mouse where you'd like to paste the text and click the middle button. That's it.
To select an entire word, double-click anywhere on that word. To select an entire line, triple-click anywhere on that line.

Configuring a two-button mouse

What if you don't have three mouse buttons? As the superuser, add the following line to /etc/rc.conf (assuming it's not already there):
moused_flags="-m 2=3"
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Get Your Daily Dose of Trivia
Brighten your day with some terminal eye candy.
As the saying goes, all work and no play makes Jack a dull boy. But what's a poor Jack or Jill to do if your days include spending inordinate amounts of time in front of a computer screen? Well, you could head over to http://www.thinkgeek.net/ to stock up on cube goodies and caffeine. Or, you could take advantage of some of the entertainments built into your operating system.
Let's start by configuring some terminal eye candy. Does your system quote you a cheery, witty, or downright strange bit of wisdom every time you log into your terminal? If so, you're receiving a fortune:
login: dru
Password:
Last login: Thu Nov 27 10:10:16 on ttyv7

"You can't have everything. Where would you put it?"
                -- Steven Wright
If you're not receiving a fortune, as the superuser type /stand/sysinstall. Choose Configure, then Distributions, and select games with your spacebar. Press Tab to select OK, then exit out of sysinstall when it is finished.
Then, look for the line that runs /usr/games/fortune in your ~/.cshrc file:
% grep fortune ~/.cshrc
/usr/games/fortune
If for some reason it isn't there, add it:
% echo '/usr/games/fortune' >> ~/.cshrc
            
Don't forget to use both greater-than signs; you don't want to erase the contents of your .cshrc file! To test your change, use the source shell command, which re-executes the contents of the file. This can come in handy if you've updated an alias and want to take advantage of it immediately:
% source ~/.cshrc
Indifference will be the downfall of mankind, but who cares?
If you'd also like to receive a fortune when you log out of your terminal, add this line to the end of your .logout file. If you don't have one, and there isn't one by default, you can create it and add this line in one step:
% echo '/usr/games/fortune' > ~/.logout
            
Note that this time I used only one greater-than sign, as I was creating the file from scratch. If the file already exists, use two greater-than signs to append your new line to the end of the existing file.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Lock the Screen
Secure your unattended terminal from prying eyes.
If you work in a networked environment, the importance of locking your screen before leaving your workstation has probably been stressed to you. After all, your brilliant password becomes moot if anyone can walk up to your logged in station and start poking about the contents of your home directory.
If you use a GUI on your workstation, your Window Manager probably includes a locking feature. However, if you use a terminal, you may not be aware of the mechanisms available for locking your terminal.
As an administrator, you may want to automate these mechanisms as part of your security policy. Fortunately, FreeBSD's screen locking mechanism is customizable.
FreeBSD comes with lock (and it's available for NetBSD and OpenBSD). Its default invocation is simple:
% lock
Key: 1234
Again: 1234
lock /dev/ttyv6 on genisis. timeout in 15 minutes.
time now is Fri Jan 2 12:45:02 EST 2004
Key:
Without any switches, lock will request that the user input a key which will be used to unlock the terminal. This is a good thing, as it gives the user an opportunity to use something other than her login password. If the user tries to be smart and presses Enter (for an empty password), the lock program will abort.
Once a key is set, it is required to unlock the screen. If a user instead types Ctrl-c, she won't terminate the program. Instead, she'll receive this message:
Key: lock: type in the unlock key. timeout in 10:59 minutes
Did you notice that timeout value of 15 minutes? At that time, the screen will unlock itself, which sorta diminishes the usefulness of locking your screen. After all, if you run into your boss in the hall, your 5-minute coffee break might turn into a 25-minute impromptu brainstorming session.
To lock the terminal forever, or at least until someone types the correct key, use the -n switch. If the system is a personal workstation,
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Create a Trash Directory
Save "deleted" files until you're really ready to send them to the bit bucket.
One of the first things Unix users learn is that deleted files are really, really gone. This is especially true at the command line where there isn't any Windows-style recycling bin to rummage through should you have a change of heart regarding the fate of a removed file. It's off to the backups! (You do have backups, don't you?)
Fortunately, it is very simple to hack a small script that will send removed files to a custom trash directory. If you've never written a script before, this is an excellent exercise in how easy and useful scripting can be.
Since a script is an executable file, you should place your scripts in a directory that is in your path. Remember, your path is just a list of directories where the shell will look for commands if you don't give them full pathnames. To see your path:
% echo $PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/
local/bin:/usr/X11R6/bin:/home/dru/bin
In this output, the shell will look for executables in the bin subdirectory of dru's home directory. However, it won't look for executables placed directly in my home directory, or /home/dru. Since bin isn't created by default, I should do that first:
% cd
% mkdir bin
            
As I create scripts, I'll store them in /home/dru/bin, since I don't have permission to store them anywhere else. Fortunately, no one else has permission to store them in my bin directory, so it's a good match.
The scripts themselves contain at least three lines:
#!/bin/sh
# a comment explaining what the script does
the command to be executed
The first line indicates the type of script by specifying the program to use to execute the script. I've chosen to use a Bourne script because that shell is available on all Unix systems.
Your script should also have comments, which start with the # character. It's surprising how forgetful you can be six months down the road, especially if you create a lot of scripts. For this reason, you should also give the script a name that reminds you of what it does.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Customize User Configurations
Now that you know how to set up a useful environment for yourself, it's time to share the wealth.
It's very easy for a system administrator to ensure that each newly created user starts out with the same configuration files. For example, every user can receive the same customized prompt, shell variables, or hotkeys.
Whenever you create a new user, several default (and hidden, or dot, files) are copied into the new user's home directory. In FreeBSD, the source of these files is /usr/share/skel/. Any customizations you make to these files will be seen by all subsequently created users. Do note that you'll have to manually copy over any modified files to existing users.
It's useful to understand these files, as they apply to every user you create. Depending upon your needs, you'll probably end up removing some of the defaults, customizing others, and even adding a few of your own.
Let's take a quick tour of the default files:
% ls -l /usr/share/skel
total 24
drwxr-xr-x   2 root  wheel  512 Jul 28 16:09 ./
drwxr-xr-x  27 root  wheel  512 Jul 28 16:06 ../
-rw-r--r--   1 root  wheel  921 Jul 28 16:09 dot.cshrc
-rw-r--r--   1 root  wheel  248 Jul 28 16:09 dot.login
-rw-r--r--   1 root  wheel  158 Jul 28 16:09 dot.login_conf
-rw-------   1 root  wheel  371 Jul 28 16:09 dot.mail_aliases
-rw-r--r--   1 root  wheel  331 Jul 28 16:09 dot.mailrc
-rw-r--r--   1 root  wheel  797 Jul 28 16:09 dot.profile
-rw-------   1 root  wheel  276 Jul 28 16:09 dot.rhosts
-rw-r--r--   1 root  wheel  975 Jul 28 16:09 dot.shrc
Note that each starts with the word dot. However, when the files are copied into a user's home directory, the dots turn into literal dots (.). Also, the files in this directory are owned by root, but when a new user is created, the copied over files will change ownership as they are placed in that user's home directory.

dot.cshrc

Let's examine each default file, starting with dot.cshrc
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Maintain Your Environment on Multiple Systems
The sign of a true Unix guru is the ability to perform a task quickly when confronted with an unfamiliar shell, keyboard, terminal, window manager, or operating system.
A large part of using Unix systems effectively involves configuring a comfortable environment using familiar tools available from the Unix shell prompt. It's much easier to perform a task quickly when all of the shortcuts your fingers have learned work on the first try.
Even something as simple as setting up your prompt the way you like it can steal significant time from your productivity if you need to do it on several hosts. If you're going to spend significant time in a Unix shell, it's worth getting organized. A bit of onetime effort will reward you later, every time you sit down at the keyboard.
unison is a tool for maintaining synchronized copies of directories. I've used it to maintain a central repository of all of my dot files, shell scripts, signatures file, SpamAssassin configuration—basically any file I'd like to have available, regardless of which host I happen to be logged into.
You can install unison from the NetBSD pkgsrc collection:
# cd /usr/pkgsrc/net/unison
# make install clean
            
FreeBSD and OpenBSD ports also include net/unison.
Even better, this utility is available for most Unix and Windows platforms. See the main unison web site for details.
Whenever I configure a new Unix host or get a shell on another system, I install unison. Then, I create a directory to receive the files I've stored in the /usr/work/sync directory at host.example.com. I call the local directory ~/sync.
To synchronize those two directories:
% unison ~/sync ssh://username@host.example.com://usr/work/sync
p = /home/david/.unison; bn = .unison
Contacting server...
p = /home/david/sync; bn = sync
username@host.example.com's password:
After
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Use an Interactive Shell
Save and share an entire login session.
How many times have you either struggled with or tried to troubleshoot another user through a thorny problem? Didn't you wish you had another set of eyes behind you so you could simply type your command set, point at the troublesome output, and say, "That's the problem." Well, if you can't bring another user to your output, you can still share that real-time output using an interactive shell.
There are actually several ways to share what is happening on your screen. Let's start by recording all of your input and output to a file. Then we'll see how we can also allow another user to view that output from another terminal.
Your BSD system comes with the script command which, not surprisingly, allows you to script your session. This command is extremely simple to use. Simply type script:
% script
Script started, output file is typescript
By default, script will create an output file named typescript in your current directory. If you prefer, you can specify a more descriptive name for your script file:
% script configure.firewall.nov.11.2003
Script started, output file is configure.firewall.nov.11.2003
Regardless of how you invoke the command, a new shell will be created. This means that you will see the MOTD and possibly a fortune, and your .cshrc will be reread.
You can now carry on as usual and all input and output will be written to your script file. When you are finished, simply press Ctrl-d. You will see this message:
Script done, output file is configure.firewall.nov.11.2003
If you've ended a script and decide later to append some more work to a previous session, remember the -a (append) switch:
% script -a configure.firewall.nov.11.2003
            
This will append your current scripting session to the named file.
I find script extremely useful, especially when I'm learning how to configure something for the first time. I can easily create a written record of which commands I used, which commands were successful, and which commands caused which error messages. It also comes in handy when I need to send an error message to a mailing list or a program's maintainer. I can simply copy or attach my script file into an email.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Use Multiple Screens on One Terminal
Running a graphical environment is great. You can have numerous applications and utilities running, and you can interact with all of them at the same time. Many people who have grown up with a GUI environment look down upon those poor souls who continue to work in a terminal console environment. "After all," they say, "you can only do one thing at a time and don't get the same information and control that you have in a desktop environment."
It's true; they do say those things. (I am curious to know who they are, however.)
It's also true that the utility of a graphical environment diminishes when you need to administer machines remotely. Do you really want to squander network bandwidth just to maintain a GUI session?
Here are some more questions to ask yourself regarding remote administration:
  • Are you worried about making your services vulnerable just so you can administer them across the Internet?
  • Do you want a secure connection?
  • Do you want to run multiple terminal sessions from a single login?
  • Do you want to be able to password protect your session to prevent unauthorized access?
  • Do you want multiple windows with labels and of different sizes?
  • Do you want to copy and paste between the windows?
  • Are you prepared to lose a connection to your remote machine in the middle of a critical operation?
  • Would you like to be able keep the session active even after you've lost it or you've logged off the remote server?
  • Do you want to take over a session that you launched from another machine?
  • Would you like to keep a hardcopy log of your sessions?
You are indeed a poor soul if you've reconciled yourself to the standard
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Dealing with Files and Filesystems
Now that you're a bit more comfortable with the Unix environment, it's time to tackle some commands. It's funny how some of the most useful commands on a Unix system have gained themselves a reputation for being user-unfriendly. Do find, grep, sed, tr, or mount make you shudder? If not, remember that you still have novice users who are intimidated by—and therefore aren't gaining the full potential of—these commands.
This chapter also addresses some useful filesystem manipulations. Have you ever inadvertently blown away a portion of your directory structure? Would you like to manipulate /tmp or your swap partition? Do your Unix systems need to play nicely with Microsoft systems? Might you consider ghosting your BSD system? If so, this chapter is for you.
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.
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 (
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
Now that you're a bit more comfortable with the Unix environment, it's time to tackle some commands. It's funny how some of the most useful commands on a Unix system have gained themselves a reputation for being user-unfriendly. Do find, grep, sed, tr, or mount make you shudder? If not, remember that you still have novice users who are intimidated by—and therefore aren't gaining the full potential of—these commands.
This chapter also addresses some useful filesystem manipulations. Have you ever inadvertently blown away a portion of your directory structure? Would you like to manipulate /tmp or your swap partition? Do your Unix systems need to play nicely with Microsoft systems? Might you consider ghosting your BSD system? If so, this chapter is for you.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
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.
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:
% which -a xmms
-a: Command not found.
/usr/X11R6/bin/xmms

% which which
which: shell built-in command.
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
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Get the Most Out of grep
You may not know where its odd name originated, but you can't argue the usefulness of grep.
Have you ever needed to find a particular file and thought, "I don't recall the filename, but I remember some of its contents"? The oddly named grep command does just that, searching inside files and reporting on those that contain a given piece of text.
Suppose you wish to search your shell scripts for the text $USER. Try this:
% grep -s '$USER' *
add-user:if [ "$USER" != "root" ]; then
bu-user:  echo "  [-u user] - override $USER as the user to backup"
bu-user:if [ "$user" = "" ]; then user="$USER"; fi
del-user:if [ "$USER" != "root" ]; then
mount-host:mounted=$(df | grep "$ALM_AFP_MOUNT/$USER")
.....
mount-user:  echo "  [-u user] - override $USER as the user to backup"
mount-user:if [ "$user" = "" ]; then user="$USER"; fi
In this example, grep has searched through all files in the current directory, displaying each line that contained the text $USER. Use single quotes around the text to prevent the shell from interpreting special characters. The -s option suppresses error messages when grep encounters a directory.
Perhaps you only want to know the name of each file containing the text $USER. Use the -l option to create that list for you:
% grep -ls '$USER' *
add-user
bu-user
del-user
mount-host
mount-user
What if you're more concerned about how many times a particular string occurs within a file? That's known as a relevance search . Use a command similar to:
% grep -sc '$USER' * | grep -v ':0' | sort  -k 2 -t : -r
mount-host:6
mount-user:2
bu-user:2
del-user:1
add-user:1
How does this magic work? The -c flag lists each file with a count of matching lines, but it unfortunately includes files with zero matches. To counter this, I piped the output from grep into a second grep, this time searching for ':0' and using a second option,
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Manipulate Files with sed
If you've ever had to change the formatting of a file, you know that it can be a time-consuming process.
Why waste your time making manual changes to files when Unix systems come with many tools that can very quickly make the changes for you?
Suppose you need to remove the blank lines from a file. This invocation of grep will do the job:
% grep -v '^$' letter1.txt > tmp ; mv tmp letter1.txt
            
The pattern ^$ anchors to both the start and the end of a line with no intervening characters—the regexp definition of a blank line. The -v option reverses the search, printing all nonblank lines, which are then written to a temporary file, and the temporary file is moved back to the original.
grep must never output to the same file it is reading, or the file will end up empty.
You can rewrite the preceding example in sed as:
% sed '/^$/d' letter1.txt > tmp ; mv tmp letter1.txt
            
'/^$/d' is actually a sed script. sed's normal mode of operation is to read each line of input, process it according to the script, and then write the processed line to standard output. In this example, the expression '/^$/ is a regular expression matching a blank line, and the trailing d' is a sed function that deletes the line. Blank lines are deleted and all other lines are printed. Again, the results are redirected to a temporary file, which is then copied back to the original file.
sed can also do the work of grep:
% sed -n '/$USER/p' *
            
This command will yield the same results as:
% grep '$USER' *
            
The -n (no-print, perhaps) option prevents sed from outputting each line. The pattern /$USER/ matches lines containing $USER, and the p function prints matched lines to standard output, overriding -n.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Format Text at the Command Line
Combine basic Unix tools to become a formatting expert.
Don't let the syntax of the sed command scare you off. sed is a powerful utility capable of handling most of your formatting needs. For example, have you ever needed to add or remove comments from a source file? Perhaps you need to shuffle some text from one section to another.
In this hack, I'll demonstrate how to do that. I'll also show some handy formatting tricks using two other built-in Unix commands, tr and col.
sed allows you to specify an address range using a pattern, so let's put this to use. Suppose we want to comment out a block of text in a source file by adding // to the start of each line we wish to comment out. We might use a text editor to mark the block with bc-start and bc-end:
% cat source.c
  if (tTd(27, 1))
    sm_dprintf("%s (%s, %s) aliased to %s\n",
        a->q_paddr, a->q_host, a->q_user, p);
  bc-start
    if (bitset(EF_VRFYONLY, e->e_flags))
  {
    a->q_state = QS_VERIFIED;
    return;
  }
  bc-end
  message("aliased to %s", shortenstring(p, MAXSHORTSTR));
and then apply a sed script such as:
% sed '/bc-start/,/bc-end/s/^/\/\//' source.c
            
to get:
if (tTd(27, 1))
    sm_dprintf("%s (%s, %s) aliased to %s\n",
        a->q_paddr, a->q_host, a->q_user, p);
  //bc-start
  //  if (bitset(EF_VRFYONLY, e->e_flags))
  //  {
  //      a->q_state = QS_VERIFIED;
  //      return;
  //  }
  //bc-end
message("aliased to %s", shortenstring(p, MAXSHORTSTR));
The script used search and replace to add // to the start of all lines (s/^/\/\//) that lie between the two markers (/bc-start/,/bc-end/). This will apply to every block in the file between the marker pairs. Note that in the sed script, the / character has to be escaped as \/ so it is not mistaken for a delimiter.
When we need to delete the comments and the two
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Delimiter Dilemma
Deal with double quotation marks in delimited files.
Importing data from a delimited text file into an application is usually painless. Even if you need to change the delimiter from one character to another (from a comma to a colon, for example), you can choose from many tools that perform simple character substitution with great ease.
However, one common situation is not solved as easily: many business applications export data into a space- or comma-delimited file, enclosing individual fields in double quotation marks. These fields often contain the delimiter character. Importing such a file into an application that processes only one delimiter (PostgreSQL for example) may result in an incorrect interpretation of the data. This is one of those situations where the user should feel lucky if the process fails.
One solution is to write a script that tracks the use of double quotes to determine whether it is working within a text field. This is doable by creating a variable that acts as a text/nontext switch for the character substitution process. The script should change the delimiter to a more appropriate character, leave the delimiters that were enclosed in double quotes unchanged, and remove the double quotes. Rather than make the changes to the original datafile, it's safer to write the edited data to a new file.
The following algorithm meets our needs:
  1. Create the switch variable and assign it the value of 1, meaning "nontext". We'll declare the variable tswitch and define it as tswitch = 1.
  2. Create a variable for the delimiter and define it. We'll use the variable delim with a space as the delimiter, so delim = ' '.
  3. Decide on a better delimiter. We'll use the tab character, so new_delim = '\t'.
  4. Open the datafile for reading.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
DOS Floppy Manipulation
Bring simplicity back to using floppies.
If you're like many Unix users, you originally came from a Windows background. Remember your initial shock the first time you tried to use a floppy on a Unix system? Didn't Windows seem so much simpler? Forever gone seemed the days when you could simply insert a floppy, copy some files over, and remove the disk from the drive. Instead, you were expected to plunge into the intricacies of the mount command, only to discover that you didn't even have the right to use the floppy drive in the first place!
There are several ways to make using floppies much, much easier on your FreeBSD system. Let's start by taking stock of the default mechanisms for managing floppies.
Suppose I have formatted a floppy on a Windows system, copied some files over, and now want to transfer those files to my FreeBSD system. In reality, that floppy is a storage media. Since it is storing files, it needs a filesystem in order to keep track of the locations of those files. Because that floppy was formatted on a Windows system, it uses a filesystem called FAT12.
In Unix, a filesystem can't be accessed until it has been mounted. This means you have to use the mount command before you can access the contents of that floppy. While this may seem strange at first, it actually gives Unix more flexibility. An administrator can mount and unmount filesystems as they are needed. Note that I used the word administrator. Regular users don't have this ability, by default. We'll change that shortly.
Unix also has the additional flexibility of being able to mount different filesystems. In Windows, a floppy will always contain the FAT12 filesystem. BSD understands floppies formatted with either FAT12 or UFS, the Unix File System. As you might expect from the name, the UFS filesystem is assumed unless you specify otherwise.
For now, become the superuser and let's pick apart the default invocation of the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Access Windows Shares Without a Server
Share files between Windows and FreeBSD with a minimum of fuss.
You've probably heard of some of the Unix utilities available for accessing files residing on Microsoft systems. For example, FreeBSD provides the mount_smbfs and smbutil utilities to mount Windows shares and view or access resources on a Microsoft network. However, both of those utilities have a caveat: they require an SMB server. The assumption is that somewhere in your network there is at least one NT or 2000 Server.
Not all networks have the budget or the administrative expertise to allow for commercial server operating systems. Sure, you can install and configure Samba, but isn't that overkill for, say, a home or very small office network? Sometimes you just want to share some files between a Windows 9x system and a Unix system. It's a matter of using the right-sized tool for the job. You don't bring in a backhoe to plant flowers in a window box.
If your small network contains a mix of Microsoft and Unix clients, consider installing Sharity-Light on the Unix systems. This application allows you to mount a Windows share from a Unix system. FreeBSD provides a port for this purpose (see the Sharity-Light web site for other supported platforms):
# cd /usr/ports/net/sharity-light
# make install clean
            
Since Sharity-Light is a command-line utility, you should be familiar with UNC or the Universal Naming Convention. UNC is how you refer to Microsoft shared resources from the command line. A UNC looks like \\ NetBIOSname \ sharename. It starts with double backslashes, then contains the NetBIOS name of the computer to access and the name of the share on that computer.
Before using Sharity-Light, you need to know the NetBIOS names of the computers you wish to access. If you have multiple machines running Microsoft operating systems, the quickest way to view each system's name is with
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deal with Disk Hogs
Fortunately, you no longer have to be a script guru or a find wizard just to keep up with what is happening on your disks.
Think for a moment. What types of files are you always chasing after so they don't waste resources? Your list probably includes temp files, core files, and old logs that have already been archived. Did you know that your system already contains scripts capable of cleaning out those files? Yes, I'm talking about your periodic scripts.
You'll find these scripts in the following directory on a FreeBSD system:
% ls /etc/periodic/daily | grep clean
100.clean-disks
110.clean-tmps
120.clean-preserve
130.clean-msgs
140.clean-rwho
150.clean-hoststat
Are you using these scripts? To find out, look at your /etc/periodic.conf file. What, you don't have one? That means you've never tweaked your default configurations. If that's the case, copy over the sam