Create a directory and cd into it with one command

April 13, 2016   

I don’t know about you but I tend to create directories from terminal often, and when I do, cding into them is the first thing I’ll do after creating the directory.

This is what it usually looks like

mkdir -p foo/bar/baz
cd foo/bar/baz

This can get quite verbose. You may have also tried this pro tip:

mkdir -p foo/bar/baz
cd !$ # press <TAB> to expand !$ to the last argument of the previous command

But I think this can be even simpler and easier. Add the following to your .zshrc or .bashrc:

mkcd() {
  mkdir -p $1 && cd $1
}

Now source .zshrc/.bashrc and simply run:

mkcd foo/bar/baz

BOOM 💥


About the Author
Responsive image

I'm Dorian Karter, a Full Stack Software Consultant.

I work at Hashrocket, a top web development consultancy based in Chicago.

Have a look at some of my open source projects.

Contact me at @dorian_escplan.