Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I find myself constantly typing (using tab, of course) absurd paths like

cd path/to/the/thing\ that\ I\ need/python/proj/eraseme

Aside from doing an ln -s (or some other type of ln?), is there any other way to get around faster? Also, if the solution is to use ln, is there some standard way/place to put the links so a not to clutter my ~ directory?

I'm not asking for shortcuts only: any solution that helps with the problem of "how to get around" would help.

share|improve this question
see also… – mrucci May 16 '10 at 20:58
@mrucci nice, thanks for that. – Dan Rosenstark May 17 '10 at 9:35
up vote 11 down vote accepted

In bash, the $CDPATH environment variable contains a list of paths to be searched for directories when using cd.

CDPATH=".:~/long path to my projects"
cd eraseme
share|improve this answer
wow. that could be the coolest thing I've seen yet. I'll have to give it a spin. thanks! – Dan Rosenstark May 16 '10 at 19:50
I think that's probably way more likely to cause confusion than an alias. – Stephen May 16 '10 at 20:21
Depends on whether you can get used to it. I mean, since it's something you do for yourself and not something you impose on other people, there's no harm in trying. – David Z May 16 '10 at 23:42
@Stephen: the reason I marked this as best answer is because if you combine this with a small script -- addcd, let's say -- it could be very powerful and you could avoid confusion entirely. Or you could add just one other path where you put your lns. This is really useful, in fact. – Dan Rosenstark May 17 '10 at 8:06
@Ignacio Vazques-Abrams, how can I get my autocomplete (tabbing) to recognize the newly added paths as well? Without that this doesn't get me very far. – Dan Rosenstark May 17 '10 at 9:14

Some people use aliases for this purpose. Try this for example:

alias cderaseme='cd /home/user/whatever/path/to/the/thing\ that\ I\ need/python/proj/eraseme'

Now you can type cderaseme every time you want to go to that directory. The alias will only be valid for the current terminal session, so put it in your .bashrc to keep it.

share|improve this answer
cool, good idea. – Dan Rosenstark May 16 '10 at 19:29

Some simple ideas:

  • Use tab completion.
  • Use cd to go back to your home directory. Use cd ~/<dir> to go a directory relative to your home directory.
  • Use cd - to go back to the last directory. I.e. with this command you can switch back and forth between two directories.
  • Use pushd <dir> to go to a directory and remember to previous location on the directory stack. Then use popd to go back (in the directory stack) to the previous location.
  • Use relative pathes, i.e. cd ../<dir>, instead of absolute paths.
  • Use cd !$ to go to the directory mentioned in the last argument of the previous command (depends on your shell). Example: mkdir /tmp/dir followed by cd !$ and you're current directory is /tmp/dir.
share|improve this answer
thanks, a few of those were new to me. – Dan Rosenstark May 16 '10 at 19:30
You can also use globbing while typing out the paths. Asterisk (*) is particularly useful -- like using a tab complete without having to pick alternatives (as long as something further down the path makes it unambiguous). – mpez0 May 17 '10 at 1:13
@mpez0 before reading your comment, I tried that out today. Didn't know that it works with multiple paths, it's actually very cool. – Dan Rosenstark May 17 '10 at 9:17

zsh has slightly better completion--you can just type few characters of each directory and press tab to get all of them expanded:

$ cd p/t/t/t/p/p/er<tab>

Also there are some utilities that can remember where do you often go and try guessing, or just behave like a smarter version of cd, f.e. cdargs, wcd, apparix, kcd... never used them, I always just used zsh completion.

share|improve this answer
interesting. I'll have to study up on why I don't use zsh (or else switch). – Dan Rosenstark May 16 '10 at 23:35
Even on bash, though, I can do cd /d*/j*/g* and if it's unique it'll get there. Not bad for a start. – Dan Rosenstark May 17 '10 at 9:15

I usually create a shell script called something like 'mycd', which I can pass parameters to. Something like this:

# Shell script to CD into various locations.
if   [ "$1" == "myhome"  ]  ; then cd ~;
elif [ "$1" == "mypref"  ]  ; then cd ~/Library/Preferences;
elif [ "$1" == "mylib"  ]   ; then cd ~/Library;
elif [ "$1" == "syslib" ]   ; then cd /System/Library;
elif [ "$1" == "--help" ]   ; then
  echo "Usage: $0 location, which can be one of"
  echo "myhome = My home dir."
  echo "mypref = My Preferences dir."
  echo "mylib  = My Libraries dir."
  echo "syslib = System library."
  echo "--help = Show this message."
else echo "$0: $1 not known.";

And then in the alias file, put an entry like:

alias mycd='. /path/to/mycd'

Then I can just call it with something like mycd mylib and it will take me straight there.

Similar to the list of aliases mentioned above, but this collects them all in one place and it gives help text if I need to be reminded what places I've stored.

share|improve this answer
+1 I like the code, but the end result is a tiny bit clumsy. – Dan Rosenstark May 17 '10 at 9:16

Try this:
a() { alias $1=cd\ $PWD; }
This creates a little function that makes it easy to create new aliases for switching directories.

share|improve this answer
damn that is CUTE, thanks for that. – Dan Rosenstark May 17 '10 at 9:28
What would be the best way to get these to persist, though? I can have a add commands to the end of a shell script, but I can't manage to affect the current shell environment ever. This problem, if you will: – Dan Rosenstark May 17 '10 at 9:35

Been goofing around with some of these answers today. What's interesting is some combination of globbing with using tab on the Bash shell. For instance:

cd /p*/t*/*need*/py*

and hit tab and if it's unique it expands, otherwise it gives you choices.

share|improve this answer

On Mac OS X you (using /bin/bash) can use all the typical terminal cd commands and cdpath information.

There is a nice trick though. You can drag a folder from the Finder into the terminal and the full path will be displayed. That makes it easier for one-off complicated file lengths to quickly and visually be entered in the terminal.

share|improve this answer
true. I use PathFinder (love/hate relationship) which does that, and also I added a thing to the Finder a while back to tell me the path. – Dan Rosenstark May 18 '10 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .