Possible Duplicate:
Is there a one-liner that allows me to create a directory and move into it at the same time?

I know this is a very basic question but I can't seem to figure out what to search for to get the answer.

In linux/unix/ssh I know you can create a folder like this:

mkdir path/to/myfolder

And you can move to that folder like this:

cd path/to/myfolder

But is it possible to create it and move to it in one command, to prevent having to type the path twice?

marked as duplicate by Gilles, Renan, jasonwryan, Kevin, manatwork Sep 24 '12 at 6:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Sep 23 '12 at 23:29

This question came from our site for professional and enthusiast programmers.


Just make a function doing it for you. In bash for example:

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


mkdircd hello
  • I wouldn't be sure where to add this so that it's always available—if it's relevant, I'm using this in the context of 1) Terminal on a Mac, and 2) SSH server running CentOS. – supertrue Sep 23 '12 at 20:17
  • 1
    Add it to ~/.bashrc if you're using Bash. – Philipp Sep 23 '12 at 20:22

If you use Bash you can do:

mkdir path/to/myfolder
cd $_

The special variable $_ expands to the last parameter of the last command. Because of this it only works if you type it directly afterwards.

See here for more information: http://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters

  • alternatively, press up-arrow and edit the line to change mkdir to cd, or just type ^mkdir^cd<ENTER> – cas Sep 23 '12 at 23:53
  • 2
    !$ does the same thing. – Kevin Sep 24 '12 at 2:27

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