Have you ever read cd‘s manpage? If you haven’t, then you should if you don’t even know what cd or cd - does. The manpage is of POSIX Programmer’s Manual, so the content is not only for the Bash, but for every POSIX-compliant shell.

1   cd brings you(r) home

It means literally, like, seriously bringing you home or bringing your home to you.

/path/somewhere $ cd
~ $

I believe many people have discovered this on their own like me. You just happen to run cd without giving any argument, then you wow’d for finding out this quick way to get back to home.

I remember once I was talking to someone, who didn’t even know about this. It’s more amazed for me than for him, I wondered how on Earth this guy had been doing in order to get back to home.

2   cd - for last directory

I don’t use this argument - often, but when I need it, it’d save a lot of typing time especially you keep switching between two directory.

~ $ cd /path/to/somewhere/deep
/path/to/somewhere/deep $ # do somegthing
/path/to/somewhere/deep $ cd /path/to/other/place/way/way/deep
/path/to/other/place/way/way/deep $ # do something
/path/to/other/place/way/way/deep $ cd -
/path/to/somewhere/deep $ # now we are back
/path/to/somewhere/deep $ cd -
/path/to/other/place/way/way/deep $ # we are back to the other

Note

If you actually have a - directory, use cd ./- to get into it.

3   CDPATH is directory search path

Before I read the manpage, I had never thought of it, even I seemed to have heard of it or seen it. It’s like PATH, but only for searching directories when using cd.

~ $ mkdir foobar
~ $ CDPATH=$HOME
~ $ cd /
/ $ cd foobar
/home/login/foobar $ # bang!

You can enter the ~/foobar from anywhere, just by typing cd foobar. It’s like you can run foobar from anywhere if it’s executable and it’s searchable via PATH.

It can also be auto-completed via bash-completion. On Gentoo, run eselect bashcomp enable base to enable it.

Although I find this could be useful, but I prefer more specific way to change directory, like my own g script for quick directory switch.

4   cd !$ comes in handy

This relies on Bash’s History Expansion, so this is not for every shell. But it’s very useful when the last argument is a directory from previous command, and you need to get into for further actions. !$ is your best friend.

~ $ mkdir long/long/directory
~ $ cd !$
~/long/long/directory $