Fast Ways of Cd'ing on *nix?

Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.";
fi

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.