Is there a shortcut to mkdir foo and immediately cd into it?

91

27

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?

Edit

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"
  else
    mkdir $1 && cd $1
  fi
}

macek

Posted 2010-06-15T14:22:06.017

Reputation: 5 157

1You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body. – Andy – 2010-06-15T14:50:50.303

2(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 – 2010-06-15T16:25:43.017

Answers

46

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.

Andy

Posted 2010-06-15T14:22:06.017

Reputation: 2 959

That also works for zsh btw – Florian Loch – 2014-12-23T11:37:32.407

2Instead of \which mkdir``, just use command mkdir. – Paused until further notice. – 2010-06-15T14:33:30.647

1Thanks 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 – 2010-06-15T14:49:06.610

2command 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. – user1686 – 2010-06-15T18:39:25.460

96

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.

kzh

Posted 2010-06-15T14:22:06.017

Reputation: 3 213

3Can you explain what $_ is? Newbie here. – arg20 – 2014-06-25T15:16:33.133

@NabilKadimi Did you mean that <kbd>Alt<kbd>+<kbd>⬆<kbd> works well in Zsh? Actually, with zsh 5.4.2 and on-my-zsh, Alt+⬆ gives me character 'A'. – Weekend – 2019-04-09T05:58:55.357

+1 for the $_ -- Stupid Sexy Flanders! – cig0 – 2019-06-29T22:05:27.160

4This works in zsh too. – Nabil Kadimi – 2014-02-16T10:01:42.000

24

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

Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet

Avijit Gupta

Posted 2010-06-15T14:22:06.017

Reputation: 341

1Can you explain this in more detail? – bwDraco – 2015-07-13T05:47:14.797

@bwDraco type take, then you will get take is a shell function from /home/username/.oh-my-zsh/lib/functions.zsh. vi the file then you get function take() { mkdir -p $@ && cd ${@:$#} } – Weekend – 2019-04-09T08:59:10.853

11

What about:

$ mkdir newdirname; cd $_

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

Alex

Posted 2010-06-15T14:22:06.017

Reputation: 119

27The point of && is that cd will not be executed if the mkdir command fails – slhck – 2012-03-14T09:29:48.807

1@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 – 2013-02-02T09:13:37.280

7

You can try something like this:

#!/bin/sh
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.

petersohn

Posted 2010-06-15T14:22:06.017

Reputation: 2 554

4how 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 Kaplun – 2013-10-13T16:15:57.783

5

$echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
$mkcd < pathtofolder/foldername >

mshameers

Posted 2010-06-15T14:22:06.017

Reputation: 151

2

Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:

function mkcd
    mkdir -pv $argv;
    cd $argv;
end

The -pv tag allows for the creation of directories with sub-directories.

Alex Fenwood Hughes

Posted 2010-06-15T14:22:06.017

Reputation: 21

0

I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :

function mkdir
{
  command mkdir $1 && cd $1
}

So I changed it and now its working great!

function mkcd
{
  command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
}

user5723841

Posted 2010-06-15T14:22:06.017

Reputation: 121

0

Depending on the desired outcome if the directory already exists.

Fail if directory already exists

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

Change directory regardless

mkcd() {
    mkdir $1 ; cd $1
}

Usage mkcd some/path/to/my/dir

bingles

Posted 2010-06-15T14:22:06.017

Reputation: 121