You are on page 1of 5

Chapter 1 C H A P T E R O N E

Customizing the User
Hacks 1-12

Users of open source ( 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 accomplish-
ing 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 solu-
tion, 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 disas-
ters. 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.

H A C K Get the Most Out of the Default Shell Hack #1

#1 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


If you’re new to the command line or consider yourself a ter- rible typist. However. If your fingers fly faster than your eyes can read and you whiz past the right command. it means that your shell isn’t sure what you want. However.” When you need to repeat a com- mand. csh. but often its simpler variant. as they think their only choice is to try typing out the whole thing all over again. For example. the system will beep because multiple commands start with so. It can be painful watching some people type out a long com- mand only to have it fail because of a typo. NetBSD and OpenBSD also ship with the C shell as their default shell. No wonder some people hate the command line! Tab activates auto-completion.HACK #1 Get the Most Out of the Default Shell right at home. if I want to run sockstat and type: % so then press my Tab key. simply press your up arrow until you find the desired command. read on. If there is a golden rule to computing. it should be: “You should never have to type a command more than once. you can recognize me in a crowd. both NetBSD and OpenBSD provide a tcsh pack- age in their respective package collections. simply use the down arrow to go in the other direction. It’s even worse if they haven’t heard about history. if I add one more letter: % soc and try again. Then. However. which doesn’t support all of the tricks provided in this hack. tcsh uses the up and down arrow keys to scroll through your command his- tory. 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. it is not always the same tcsh. The Tab key was specifically designed for both the lazy typist and the terri- ble speller. press Enter and think of all the keystrokes you just saved yourself. and Tab. the system will fill in the command for me: % sockstat 2 | Chapter 1. This means that if you type enough letters of a recognizable command or file. In fact. down arrow. However. History and Auto-Completion I hate to live without three keys: up arrow. Customizing the User Environment . tcsh will fill in the rest of the word for you. if you instead hear a beep when you press the Tab key. Unix might be a whole lot easier than you think.

If you work in the Cisco or PIX IOS systems. my last command will be displayed at the prompt: % vi mydocs/today/verylongfilename I’d now like to double-check how many words and lines are in that file by running this command: % wc mydocs/today/verylongfilename I could pound on the backspace key until I get to the vi portion of the com- mand. Sometimes you would like your cursor to go to the end of the command. you can press Enter. It doesn’t matter where your cursor happens to be. Once your command looks like it should. Let’s say I want to run the word count command on two files. I don’t have to use my right arrow to go to the end of the command in order to press Enter and execute the command. Customizing the User Environment | 3 . If I press my up arrow. Did you know that the cd command also includes some built-in shortcuts? You may have heard of this one: to return to your home directory quickly. For a mnemonic device. simply type: % cd Chapter 1. the cursor will jump to the end of the com- mand. Finally. That would bring me to the very beginning of that command so I could replace the vi with wc. remember that just as a is the first letter of the alphabet. so I can type in the rest of the desired command. all of the previ- ous tricks work at the IOS command line. but it would be much easier to hold down the Ctrl key and press a. Suppose I’ve just finished editing a document. what if you’re in the middle of a long command and decide you’d like to start from scratch. Remember that e is for end. HACK Get the Most Out of the Default Shell #1 Editing and Navigating the Command Line There are many more shortcuts that can save you keystrokes. it also represents the first letter of the command at a tcsh prompt. and just get your prompt back? Simply hold down Ctrl and press u for undo. erase what you’ve typed. and right now my cursor is at the first c in this command: % wc mydocs/today/verylongfilename If I hold down Ctrl and press e.

there is a very quick solution. “Useful tcsh Shell Configuration File Options” [Hack #2] will take care of that. you really don’t want to type out that long direc- tory path again. Customizing the User Environment . If that’s the case. but what if you want to change to a different previ- ous directory? Let’s say that you start out in the /usr/share/doc/en_US. But why search yourself when ! can search for you? 4 | Chapter 1. You now want to repeat something you did half an hour ago. If you’re anything like me. but chances are you originally navigated into that deep directory structure one directory at a time. ISO8859-1/books/handbook directory. What.HACK #1 Get the Most Out of the Default Shell That’s very convenient. let’s fine-tune some of these hacks. You could keep tapping your up arrow until you come across the command. Learning from Your Command History Now that you can move around fairly quickly. The ! (or bang!) character has several other useful applications for dealing with previously issued commands. Remember that document I created? Instead of using the history to bring up my previous command so I could edit it. then use cd to change to the /usr/ X11R6/etc/X11 directory. Now you want to go back to that first directory. Suppose you’ve been extremely busy and have issued several dozen commands in the last hour or so. Since that command was: % vi mydocs/today/verylongfilename it replaced the !$ in my new command with the very long filename from my previous command. you could pick it out of your history. I might have found it quicker to type this: % wc !$ wc mydocs/today/verylongfilename 19 97 620 mydocs/today/verylongfilename The !$ tells the shell to take the last parameter from the previous command. your prompt isn’t changing to indicate your current working directory? Don’t worry. it would probably take you longer to pick each piece out of the history than it would be to just type the command manually. Simply type: % cd - Repeat that command and watch as your prompt changes between the first and the second directory. Fortunately. How many times have you found yourself repeating commands just to alter them slightly? The following scenario is one example. Sure.

If I type: % ls -l b then hold down the Ctrl key while I press d: backups/ bin/ book/ boring. this command will do the same thing: % h Each command in this history will have a number. tcsh would find that instead. Or perhaps you just find it frustrating typing one letter. and then my prompt will return my cursor to what I’ve already typed. if I want to view the size and permissions of boring. I could give ! enough letters to figure out which command to pick out from my history: $ !ma ! will pick out the most recently issued command that begins with ma. and so on until auto-complete works. You can specify a com- mand by giving ! the associated number. In this example. typing another letter. tabbing. In this example. I’ll ask tcsh to reissue the mailstats command: % h 165 16:51 mailstats 166 16:51 sockstat 167 16:52 telnet localhost 25 168 16:54 man sendmail % !165 Silencing Auto-Complete The last tip I’ll mention is for those of you who find the system bell irritat- ing. HACK Get the Most Out of the Default Shell #1 For example. See Also • man tcsh Chapter 1. I’ll leave it up to your own imagination to decide what the d stands for.jpg % ls -l b I’ll be shown all of the b possibilities in my current directory.jpg. tabbing. If I had issued a man command sometime after mailstats command. you can view your history by simply typing: % history If you’re really lazy. Customizing the User Environment | 5 . if I’d like to repeat the command mailstats. I’ll need to type up to here: % ls -l bor before I press the Tab key. This would fix it though: % !mai If you’re not into trial and error.