Unix Tip: Favorite Tricks, Take Two
Send in your Unix questions today!
See additional Unix tips and tricks
Several weeks ago, I shared some of my favorite Unix "tricks" and invited readers to tell me about some of theirs. Quite a few people responded, so this week's column reflects some of the submitted favorites.
Easy Directory Maneuvering
Several of the ideas submitted involved ways to facilitate moving around in the Unix file system. In particular, how to move from one directory to the next with the least amount of typing and how to most easily remember where you've been and how to get back to some of the more strategic directories that you have been working in.
Jeffrey White mentioned the Korn shell's ability to replace a portion of the current path in a cd command. For example, if you are positioned within a directory named /opt/app/abc/files/conf and you want to move into the /opt/app/xyz/files/conf directory, you could carefully construct a cd command that backs you up and then moves you forward (e.g., cd ../../xyz/conf) or you could use a command like the cd command shown here (third line):
$ pwd
/opt/app/abc/files/conf
$ cd abc xyz
$ pwd
/opt/app/xyz/files/conf
If you are moving between project or customer subdirectories, this sort of cd command is rather intuitive. Think about "cd CT MD" for moving from one state directory to another. What could seem more natural?
Steve Lloyd uses an alias to move from one directory to another. By capturing pathnames using meaningful variable names, he can move between directories that are important to a particular task. The alias he uses looks like this:
alias mark 'set \!*=`echo $cwd`'
Once this alias is set up, Steve can do things like this:
$ cd /opt/app/abc/files/conf
$ mark abc
...
$ cd $abc
If the marked directory is one that Steve needs to return to repeatedly to accomplish a particular task, he will have made coming back to that directory again and again a lot less annoying.
If you are moving between user home directories, there's always the convenience of the ~username syntax to move you quickly to each user's home. Several people mentioned how much time this shortcut saves them.
Jared Still (of Perl for Oracle DBAs fame) has turned directory organization into something of an art with his dirs package. A set of directory stack functions load into the shell environment, giving the user tools for creating and using a directory name stack.
By creating a directory stack, Jared can move back as many directories as he has added onto his stack. For example, let's say you moved into /opt/app/abc/files/conf, issued a pushd command to add that directory to your stack. You then moved to /opt/app/xyz/files/conf and used pushd again. You could then list your stack to see where you have been:
boson> d
OLDPWD: /export/home/jdoe/opt/app/xyz/files/conf
0 /export/home/jdoe/opt/app/xyz/files/conf
1 /export/home/jdoe/opt/app/abc/files/conf
Want to return to any of the stacked directories? That's easy enough. Just type "g 0" (as in "go to 0") and you'll find yourself in the directory listed on the top of the stack or "g 1" to go to the abc equivalent. You could also save the stack or clear all of the entries from the stack with simple commands. Jared's clever functions deserve much more than a casual mention, so we'll look at them in more detail in a later column.
Easy Command Editing
While the method of command editing may vary dramatically from one shell to the next, it is one of the shell functions that users have come to appreciate for the ease with which a command can be repaired and reentered. For Todd Kocian, there is joy in being able to change a command like this:
$ find . -nome "Test*" -print
find: bad option -nome
find: path-list predicate-list
$ ^nome^name
find . -name "Test*" -print
For Glenna White, its backing through commands with the up arrow on her keyboard and fixing typos by backing up to them with the left arrow key that keeps the frustration out of typing. In any case, not having to completely retype a botched command avoids a lot of irritation on the command line.
Meaningful Command Prompts
Several people mentioned the benefit that they derived from being able to put information that was significant to them in the command prompt -- whether that was the directory name, system name or a shortened version of the path. For me, being able to keep my prompt to a minimum is pleasing because I resent a cluttered command prompt -- especially one that ends well beyond the midpoint of my terminal window. If I'm working on several different systems at once, the host name helps to keep me from typing a command on the wrong system. If I'm working on several different projects, on the other hand, I may want something like a project name or customer name in my prompt. Anytime I'm working on half a dozen things at once, reminders of which window represents which task are valuable and being able to set the reminders in my prompts works well for me.
Some readers like commands like these for including both the hostname and directory in the command prompt (csh version):
set prompt="`hostname` [$cwd]#"
alias cd 'cd \!*; set prompt="`hostname` [$cwd]#"'
Others prefer a minimalist command prompt such as "> " that don't distract from commands and responses:
export PS1="> "
What people want is to be able to set their prompts in ways most appropriate to the task at hand and their own comfort level with information or "clutter".
Thank you to everyone who responded with their favorite Unix tricks.
ITworld.com
Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!
The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!












