Hack #3-> Perform mkdir and cd Using a Single Command

Sometimes when you create a new directory, you may cd to the new
directory immediately to perform some work as shown below.

# mkdir -p /tmp/subdir1/subdir2/subdir3
# cd /tmp/subdir1/subdir2/subdir3
# pwd

Wouldn’t it be nice to combine both mkdir and cd in a single command?
Add the following to the .bash_profile and re-login.

# vi .bash_profile
function mkdircd () { mkdir -p “$@” && eval cd “\”\$$#\””;

Now, perform both mkdir and cd at the same time using a single
command as shown below:

# mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it
# pwd

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s