5

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?

supertrue
  • 151

2 Answers2

9

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

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

example

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. –  Sep 23 '12 at 20:17
  • 1
    Add it to ~/.bashrc if you're using Bash. –  Sep 23 '12 at 20:22
8

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