Skip to content

Instantly share code, notes, and snippets.

@rajeshg
Created November 23, 2010 19:01
Show Gist options
  • Save rajeshg/712300 to your computer and use it in GitHub Desktop.
Save rajeshg/712300 to your computer and use it in GitHub Desktop.
mkcd command - how to create a directory using mkdir and cd in one step
#
# mkcd command
# This is an improvised version of the mkcd command at http://superuser.com/questions/152794/is-there-a-shortcut-to-mkdir-foo-and-immediately-cd-into-it
# This function has to be added to the ~/.bashrc file
# After that you can run command like: mkdir abc, mkdir -p one/two/three
#
function mkcd {
last=$(eval "echo \$$#")
if [ ! -n "$last" ]; then
echo "Enter a directory name"
elif [ -d $last ]; then
echo "\`$last' already exists"
else
mkdir $@ && cd $last
fi
}
@pjobson
Copy link

pjobson commented Aug 18, 2017

Thanks was just looking for exactly this.

@Diamo-B
Copy link

Diamo-B commented Oct 19, 2022

Thanks man

@zefir-git
Copy link

zefir-git commented Jun 17, 2024

function mkcd() {
    mkdir -p $@ && cd $@;
}

or you can actually just use

mkdir anything && cd $_

mkdir -p allows you to create parent dirs and also doesnt error if a dir already exists, meaning that you can still mkcd even if the dir already exists (effectively just cd)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment