Make a directory and enter it with one command

I’m using the command line a lot more now that I’ve decided to finally learn Git and – powerful as it is – I’m still finding minor annoyances that are easily overcome with a little shell hacking.

Todays annoyance came from not being able to create a directory and automatically enter it with a single command. Easy:

1. Open up your shell’s config file:
[Bash Users]
nano ~/.bashsrc

[ZSH Users]

2. Add the following function:
[Bash Users]
mkcd () {
mkdir -p "$*" && cd "$*"

[ZSH Users]
mkcd () {
md "$*" && cd "$*"

3. Save / Close nano
Ctrl + O -> Enter -> Ctrl + X

4. Start a new bash session
CMD+W to close the current window, CMD+N for a new one

5. Test:
mkcd test1/test2

You should be now in the test2 directory

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.