Command Line - Create directory and change into it at the same time

December 06, 2015

The cool thing about programming is that you’re constantly discovering and seeking out faster ways of doing things. Sometimes you learn something later than you would have hoped, but better late than never! After countless times of mkdir’ing and then cd’ing into immediately after, I sought after a better way.

Before, I would do either this:

User $ mkdir /home/foo/doc/bar
User $ cd /home/foo/doc/bar

OR, even better, combine it into a single command by using the && operator:

User $ mkdir /home/foo/doc/bar && cd /home/foo/doc/bar

However, instead of typing out the long directory name or chain, you can simply replace it with $_:

User $ mkdir /home/foo/doc/bar && cd $_

It works the same, and I’m going to start using it to see if it soothes my impatience.


Patrick El-Hage

I'm Patrick El-Hage and I live and work in San Francisco. I'm also on Twitter.