Sunday 24 May 2015

Do mkdir and cd using a single command?

Most of the times, when you create a new directory, you may cd to it, to do some work.
chetna.chaudhari@Chetna:~$ mkdir -p /a/b/c
chetna.chaudhari@Chetna:~$ cd /a/b/c
chetna.chaudhari@Chetna:~$ pwd
/a/b/c

How about combining these two commands into a single one.? yes its doable. You
to add following snippet to your .bash_profile file and relaunch a shell.
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
Now do mkdir and cd using a single command
chetna.chaudhari@Chetna:~$ mkdircd /x/y/z
chetna.chaudhari@Chetna:~$ pwd
/x/y/z