Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is something I do frequently

$ mkdir foo
$ cd foo

This works as a single command, but it's more keystrokes and saves no time.

$ mkdir foo && cd foo

Is there a shortcut for this?


With the use of help below, this seems to be the most elegant answer.

# ~/.bashrc
function mkcd {
  if [ ! -n "$1" ]; then
    echo "Enter a directory name"
  elif [ -d $1 ]; then
    echo "\`$1' already exists"
    mkdir $1 && cd $1
share|improve this question
You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body. – Andy Jun 15 '10 at 14:50
(1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif"). – quack quixote Jun 15 '10 at 16:25
up vote 29 down vote accepted

I'm no Linux/bash expert, but try putting this in your .bashrc.

function mkdir
  command mkdir $1 && cd $1

PS Thanks to Dennis for using command mkdir.

share|improve this answer
Instead of `which mkdir`, just use command mkdir. – Dennis Williamson Jun 15 '10 at 14:33
Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;) – Andy Jun 15 '10 at 14:49
command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command. – grawity Jun 15 '10 at 18:39
@grawity Perfect, thanks. – Andy Jun 16 '10 at 8:04
Not sure why this is not the accepted answer—the currently accepted one doesn't even seem to work. – Erik Allik Oct 13 '13 at 16:18

The bash, zsh Shells

If you don't want another function to remember and don't mind bashisms:

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

The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:

foo bar
bar baz

The fish Shell

In the fish shell, I would type the following:

> mkdir /home/foo/doc/bar
> cd alt + ↑

The alt key combined with either the up or the down arrow key will cycle through command parameter history.

share|improve this answer
This works in zsh too. – Nabil Kadimi Feb 16 '14 at 10:01
Can you explain what $_ is? Newbie here. – arg20 Jun 25 '14 at 15:16
@arg20 I updated my answer. – kzh Jun 25 '14 at 18:04
this should be the selected answer – Herod Nov 23 '14 at 16:35
Yeah. This really should be selected. This is perfect. I can't ssh into a server and update .bashrc everytime... Thanks for this! – Jake Feb 13 '15 at 3:38

You can try something like this:

mkdir $1 && cd $1

Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.

share|improve this answer
how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script. – Erik Allik Oct 13 '13 at 16:15

What about:

$ mkdir newdirname; cd $_

It's a bit easier than using &&, combining quack quixote's and kzh's answers.

share|improve this answer
The point of && is that cd will not be executed if the mkdir command fails – slhck Mar 14 '12 at 9:29
@slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;. – moala Feb 2 '13 at 9:13

For oh-my-zsh users:
$ take 'directory_name'


share|improve this answer
Can you explain this in more detail? – bwDraco Jul 13 '15 at 5:47
@DragonLord updated the answer with the reference – Avijit Gupta Jul 13 '15 at 8:23
Maybe you should clarify this is for oh-my-zsh users specifically. take is definitely not built into zsh. – macek Jul 14 '15 at 4:17
@macek Perhaps you are right. Clarified my answer. Thanks – Avijit Gupta Jul 14 '15 at 7:02
$echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
$mkcd < pathtofolder/foldername >
share|improve this answer

Depending on the desired outcome if the directory already exists.

Fail if directory already exists

mkcd() {
    mkdir $1 && cd mkdir

Change directory regardless

mkcd() {
    mkdir $1 ; cd $1

Usage mkcd some/path/to/my/dir

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .