Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Changing between directories the easy way UNIX
When working in the Terminal, I often have to change back and forth between several directories. The tcsh shell has built-in capabilities to help with this: 'push' and 'pop' can be used instead of 'cd' when changing directories. With 'push' and 'pop', you get a stack of directories that makes it a bit easier for going back and forth. But this is not so useful if you have a more random pattern of directory use: you go here, then you go somewhere else, ... and of course, you have to remember to use 'push' and 'pop' instead of 'cd'. Often you find yourself having to type in a long directory path (but filename completion via Tab helps a lot here). So what I find convenient is the following facility which allows you to give short mnemonic names to directories and then you can use these names of your own choosing with 'cd' .

Read the rest of the article for the how-to...

First I'll show you how to use it (via examples) and then I'll show you how to implement it via a few aliases.

Suppose you find yourself frequently going to the directory /Users/fred/Documents/Projects/Acme. When you are in that directory, you issue the command:
 % save acme
This assigns "acme" as your own private short name for the directory you are in. Then, later on, when you are in some other (far-away) directory, you can go back to the Acme directory by issuing the command:
 % cd acme
After a while, you may have accumulated a number of these short names for directories and they will serve as a memory aid. Suppose you forget where the preferences files are kept and you know that you had saved a short name for this directory, but you forget what it was. You issue the command...
 % show
... and you will get a list of all the short names and their full-path equivalents. Note: the short names you supply via the 'save' command must be one word (no spaces or punctuation).

The above commands (save and show) are implemented via aliases and you can install them for your use as follows:
  1. Create an empty file called ".dirs" in your home directory via the following command:
     % touch ~/.dirs
  2. Put the following lines in your aliases file (see previous hints on how to use aliases):
    # The following aliases (save & show) are for saving frequently used directories
    # You can save a directory using an abbreviation of your choosing. Eg. save ms
    # You can subsequently move to one of the saved directories by using cd with
    # the abbreviation you choose. Eg. cd ms (Note that no '$' is necessary.)
    alias save 'sed "/set \!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo
    set \!* = \"`pwd`\" >> ~/.dirs; source ~/.dirs'
    alias sdirs 'source ~/.dirs'
    alias show 'cat ~/.dirs'
    # source the ~/.dirs file so that we get the previously set up variables
    if ( -f ~/.dirs ) source ~/.dirs
    [NOTE: Enter the "save" alias as one line; it's been broken here for readability]
You can of course change the names to something else (e.g. saveDir & showDirs) if you like.

How this works is the 'save' command creates a shell variable with the name you supply and sets its value to the full path of the current directory. The variables are stored in the file ".dirs" in your home directory. It is a built-in feature of the tcsh shell that if you do 'cd foo', the 'cd' command checks if foo is a shell variable and if so, it evaluates the variable to find the directory it is supposed to go to (no $ is needed). Of course, if there is a sub-directory named "foo", it goes there directly, not bothering about any variables.

You can edit the ".dirs" file manually if you want - this is useful for updating paths and deleting directory short names which you no longer need. The 'sdirs' command is useful if you have set up a short name in one Terminal window and you want it to be available in another already-existing window. New windows automatically get all the short names by virtue of the line that sources the ".dirs" file.

I find the directory-path variables useful for other operations as well. For example, if I need to copy a bunch of files from the current directory to the directory which I had previously given the short name "acme", I can do it with the command:
 % cp file1 file2 file3 $acme
Here the $ is necessary since (unlike 'cd') the 'cp' command does not automatically look for shell variables.
    •    
  • Currently 4.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[10,270 views]  

Changing between directories the easy way | 7 comments | Create New Account
Click here to return to the 'Changing between directories the easy way' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Great!
Authored by: bluehz on Jul 16, '02 07:57:38AM

Great tip - thx!



[ Reply to This | # ]
Great!
Authored by: dhed on Jul 16, '02 07:18:10PM

This sounds great, but I keep getting the message, "save: Command not found." when I try it. What details might I be missing? Does the file have to be executable? Should it be in the ~/bin directory? Can I use the default shell? Help!



[ Reply to This | # ]
Great!
Authored by: hayne on Jul 16, '02 11:02:45PM
dhed wrote:
I keep getting the message, "save: Command not found." when I try it.

The 'save' command is actually an alias so if you are getting the above error message, it means that you haven't installed the aliases correctly. Have you worked with aliases before? If not, you need to read the previous hints on using aliases in the shell. (search macoshints for "aliases" (without the quotes) and you will find these previous articles.

[ Reply to This | # ]
Got it!
Authored by: dhed on Jul 17, '02 12:36:10PM

Thank you!

What I really, really need is a way for me to erase my boneheaded posts so I don't look quite so stupid. :-)



[ Reply to This | # ]
I can make that available...
Authored by: robg on Aug 05, '02 01:26:25AM

Hmmm, as a "value added" service on macosxhints. Normal registered users, $10 a year ... registered users with the ability to delete their own comments ... $15,000 a year ;-).

Protection ain't cheap, ya know!?

-rob.



[ Reply to This | # ]
version for bash
Authored by: hayne on Oct 12, '04 01:11:37AM
Since the default shell for new accounts in Panther is 'bash' instead of 'tcsh', I thought it might be useful to show how to translate the above C-shell aliases for use in bash. Here is what I have in my ~/.profile file:

# The following aliases (save & show) are for saving frequently used directories
# You can save a directory using an abbreviation of your choosing. Eg. save ms
# You can subsequently move to one of the saved directories by using cd with
# the abbreviation you chose. Eg. cd ms  (Note that no '$' is necessary.)
alias sdirs='source ~/.dirs' 
alias show='cat ~/.dirs'
save () { command sed "/!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "$@
"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ; }

# Initialization for the above 'save' facility:
# source the .sdirs file:
sdirs
# set the bash option so that no '$' is required when using the above facility
shopt -s cdable_vars


[ Reply to This | # ]
Re: version for bash
Authored by: hayne on Apr 07, '06 08:05:12AM
The above 'save' function should be revised to use $@ instead of !$ - i.e. it should be:

save () { /usr/bin/sed "/$@/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "$@"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ; }


[ Reply to This | # ]