Back to snippets
linux
BashLinux

mkcd: Make Directory and Enter It

A simple Bash function to create a directory and switch into it in a single command.

We’ve all been there: you create a new directory with mkdir and then immediately have to cd into it.

mkdir new-project
cd new-project

It’s a small friction, but it adds up. Here is a simple function to do both at once.

The Solution

Add this function to your .bashrc (or .zshrc):

# Create a folder (and subfolders) and enter it immediately
mkcd() {
  mkdir -p "$1" && cd "$1"
}

How to Install

  1. Open your config file:
    nano ~/.bashrc
  2. Paste the code block above at the end of the file.
  3. Save and exit (Ctrl+O, Enter, Ctrl+X).
  4. Reload your configuration:
    source ~/.bashrc

Usage

Now you can create deep directory structures and enter them instantly:

mkcd docker/docker-compose/vaultwarden

You will find yourself inside vaultwarden immediately.