Switching Between Directories

Switching Between Directories









I Heart AWeber.com

Do you love AWeber, too?

When working at the Unix csh command line, I often find myself needing to switch back and forth between 2 directories, over and over, again and again.

The Problem

Having to "cd" back and forth gets old, quick. It's a lot of typing, even with command-completion:

UNIX% cd /etc/httpd/conf
UNIX% vi httpd.conf
UNIX% cd /etc/rc.d/init.d
UNIX% ./httpd reload
lather,rinse,repeat

Workarounds

OK, I know, if you were doing this, you probably would not quit out of Vi every time. You could just hit :w to save, and ^Z to temporarily suspend. After you checked to see if it was working (and found it needed another change), you could use "fg" to resume the Vi session -- no matter what directory you were in.

Some people like to use the History mechanism, like "!cd", to repeat past commands. But that only works if the 2 commands are different enough. Since both of these commands start with "cd", this won't work so well. I guess you could use "!123" style history commands (by history number), but then you'd have to remember those numbers, and they're different for every edit session; that's a little too annoying to remember.

You can probably think of other examples in your login sessions where you're constantly switching between 2+ directories. For example, maybe you're working in some directory and someone stops by to ask you where a certain file is, or why they cannot access some directory. You want to look around with "cd" a few times to answer their question, then jump back to exactly the place you were at before they visited.

You'll be happy to know there are some nice built-in commands in the C-shell to do this kind of thing.

Pushd, Popd, Dirs

There are three awesome commands in the csh to help with dealing with many directories.

Push a directory onto the Directory Stack:

pushd somedir

Swap current dir with last one on the Dir Stack:

pushd

Swap current dir with the directory 2 from the top of the Stack:

pushd +2

Pop a directory off of the Directory Stack, making it your current directory:

popd

Examine the directory stack:

dirs

When a directory is deleted, if it was on your directory stack, you can't pushd or popd to it anymore.

Remove a non-existent directory from the Stack:

popd +1

How I Use Them

Now, in the example above, your commands would look like this:

UNIX% cd /etc/httpd/conf
UNIX% vi httpd.conf
UNIX% pushd /etc/rc.d/init.d
UNIX% ./httpd reload

From that point on, it gets brain-dead easy:

UNIX% pushd
UNIX% vi httpd.conf
UNIX% pushd
UNIX% ./httpd reload

This works because the "pushd" command swaps between your current directory and the most recent one on your directory stack (i.e. the one you were in before you "pushd"d to a different directory). When I'm typing these commands, what I'm thinking to myself sounds like: "Go back and edit the file. Now go back and test the file."

What pushd is really doing is:

  • push your current directory on the dir stack
  • "cd" to the directory you specified on the command line

    Interruption Factor

    The next time someone interrupts you causing you to "cd" elsewhere in aggravation, try this:

    pushd /wherever/they/need/you
    ls whatever
    pushd
    

    and you're back to where you left off!

    Directory Threesome

    If you're trying to hot-foot it around 3 directories round-robin style, you'll probably use this command each time:

    pushd +2
    

    Aliases Help Even More

    Believe it or not, these commands are still too slow for me. I want to type "pod" instead of popd, and "pud" instead of pushd. It's easier and faster to type, for someone like me who can type faster than every secretary in every department I've ever worked in.

    So I set up these aliases in my .cshrc file:

    alias pud pushd
    alias pod popd
    

    Now it's even easier to toggle back and forth:

    pud /wherever/they/need/you
    ls whatever
    pud
    

    A Quick Distraction - Chording

    I type fast. Really fast. Those of you who've seen me in action know this to be true. I do chording-style typing on ordinary keyboards. In other words, I don't press individual keys; I have 10 fingers, and each finger can move (somewhat) independently of the others. So long as the next 2 or 3 letters I have to type occur on different fingers, I can press those keys "almost at the same time"; as long as the first key comes out a split second before the next one (and so on), the computer doesn't care. If you watch me when I really get going, it looks and sounds like I'm pressing multiple keys at once.

    Consider the word "the", it's one of the most frequently used words in the English language. Look at the keys for it on the ordinary Qwerty keyboard. Each letter falls on a different finger, from a touch-typing point of view:

    "t" is on your Left Hand Pointer finger,
    "h" is on your Right Hand Pointer finger,
    "e" is on your Left Hand Middle finger.
    

    That's 3 separate fingers! To type the word "the" I perform one movement, which is to press all 3 keys at the same time, but with slightly offset timing, in the proper order: T followed closely by H followed closely by E. In fact, the SPACEBAR often follows the word "The" - using your Thumb, which is another finger yet again. Yup, still 1 chord.

    Now think about "pushd" - it has 2 chords: PUS, and HD. The command "pud" has only 1 chord! It takes me twice as long to type "pushd" than "pud". A trivial difference, you say? Not when I have to type each one upwards of 500 times a day!

    IBM Keyboards Are Bad for Chording

    One note on Chording: when you get good at this, you'll begin to feel flaws in certain keyboards. All IBM keyboards I've ever used have some weird bug in them - when I type the word "the" on an IBM keyboard it almost always comes out "teh", or "hte", even though I am absolutely sure that I typed it in the proper order. It must be some kind of character-buffering issue; perhaps they mistakenly used a LIFO buffer instead of a FIFO buffer. All I know is I cannot use IBM keyboards; they drive me insane.

    Other Notes

    I am so much more productive by using pushd, popd, and dirs - built-in commands to the csh and tcsh. However, I aliased pushd to "pud" and popd to "pod", to make them easier to type.

    I quickly type both "dirs" and "jobs" commands when I sit down at any Unix command line, to get a feel of "where I'm at" in this window. It helps me see if I left any backgrounded Vi sessions (don't want to lose the work there!), and to see what other directories I had pushed that I haven't popped my way back to, yet.

    I won't close any window/shell until I've resolved those 2 things.



    Bookmark and Share


    Don't miss the latest unix tips and tricks!
    Subscribe to our low-volume mailing list:

    Privacy Policy

    See other tricks:  Web | Unix | Perl | SQL | General


    Sample Sites | Customers | Our Team | Contact Us | Tips and Tricks | Tools | Our Network | Home

    Copyright © 2006 Fastech Learning LLC, all rights reserved.
    Phone toll free 1-866-464-6688, Phoenix Metro area 480-895-6688
    Problem with this web site? please let us know