mkdir and cd

Many times, i find myself making and moving to a directory quite a bit, normally I use something like this
$ mkdir ~/dir && cd ~/dir
or
$ mkdir -p create/new/dir && cd create/new/dir
You can’t have a simple BASH script that reads “mkdir $1 && cd $1” because it would only switch to the directory while running as a child process instead of actually switching to the directory.
However there is a quick and easy way to combine the tasks, add a function mkcd() to your .bashrc.
$ cat >> ~/bashrc
function mkcd(){
[ -n “$1” ] && mkdir -p “$@” && cd “$1”;
}

mkdir and cd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s