Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

I'm seeking a way to create directory and change my present working directory to newly created directory just by using a single command. How can I do this?

i.e Instead of doing

user@Computer:~$ mkdir NewDirectory
user@Computer:~$ cd NewDirectory

I want to do

user@computer:~$ **command** NewDirectory

What can the command be?

share|improve this question
up vote 15 down vote accepted

If you really want it to be just one command, I suggest adding something like this to your .bashrc:

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

Entering md foo on the command line will then create a directory called foo and cd into it immediately afterwards. Please keep in mind, that you will have to reload your .bashrc for the changes to take effect (i.e. open a new console, or run source ~/.bashrc).

Cf. for possible alternatives, too.

share|improve this answer

mkdir "NewDirectory" && cd "NewDirectory"

  • The part behind the && will only execute if the 1st command succeeds.
  • It is called a Lists of Commands in the Bash manual.
  • There is also a shorthand version:

    mkdir "NewDirectory" && cd "$_"
  • Example from command line:

    $ false && echo "yes"
    $ true && echo "yes"
  • (edit) Add " to the commands since the directory might contain a space.

share|improve this answer
mkdir "NewDir" && cd "$_" works great than mkdir "NewDir" && cd "NewDir" as auto complete doesn't work. BTW what is "$_" ? – TheKojuEffect Jan 30 '13 at 10:13
More than that it would be quite handy if we can attach a switch to mkdir to change to new directory created. – TheKojuEffect Jan 30 '13 at 10:14
@TheKojuEffect $_ see Regarding that last one: nobody is stopping you from using an alias or a function inside .bashrc ;) – Rinzwind Jan 30 '13 at 14:39

There's no built-in function for that, but you can use shell functionality to help you not have to type the argument of the cd command again after running mkdir:

  • Type cd , then Esc . (or Alt+.) to insert the last argument from the previous command.
  • cd !$ executes cd on the last argument of the previous command.
  • Press Up to recall the previous command line, then edit it to change mkdir into cd.

You can define a simple make-and-change-directory function in your ~/.bashrc:

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

Reload your .bashrc (. ~/.bashrc) or restart bash, and now you can type mkcd new-directory.

This simple version fails in some unusual cases involving weird directory names or .. and symbolic links. Here's one that does. For explanations, see the Unix & Linux version of this question.

mkcd () {
  case "$1" in
    /*) mkdir -p "$1" && cd "$1";;
    */../*) (cd "./${1%/../*}/.." && mkdir -p "./${1##*/../}") && cd "$1";;
    ../*) (cd .. && mkdir -p "${1#.}") && cd "$1";;
    *) mkdir -p "./$1" && cd "./$1";;
share|improve this answer
My +1 is specifically for the Bash-specific references to the last command's arguments. – 0xC0000022L Feb 13 '13 at 20:06

You might also take a look at, and in general. It doesn't have the broad cover that provides. And not even by a long shot.

However I think it's a cool way to share your .bash_aliases among different installations.

share|improve this answer
mkdir -p "as/many dirs/as you/want" && cd "$_"

there is a solution listed above (same as my solution but with no -p option), but I think it has a few pitfalls.

man mkdir
-p, --parents
       no error if existing, make parent directories as needed

This gives us two benefits:

  1. We can create as many subdirectories as needed in one go
  2. If the directory exists, it will not throw an error, and we will be still able to switch to the existing directory.

&& cd "$_" will change to the newly created directory, since $_ hold the value returned my mkdir

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.