making a directory and changing to it in one command (linux)

RSpec Core 2.6 – RSpec – Relish Credit goes to lifehacker for this useful tidbit. Anyone who’s used linux for more than a short while will notice that one task you find yourself doing quite often is making a directory, then changing to that directory immediately after. Normally this is accomplished by the pair of commands

mkdir dir
cd dir

After a while this becomes tiresome though, and you start to wonder, isn’t there an easier way…? Maybe a command that does this simple task for us in one shot? The answer is no, there isn’t, but as with anything in linux, it can be made.

1. Open up your shell configuration file, .bashrc or .profile if you’re using bash, located in your home directory. Note, if you want to have this command available to your sudo account, change it in the root directory, but be warned–changing the shell configuration file for root can be potentially hazardous as a security risk or worse a PC-crippling problem if you unintentionally botch the edits.

2. Add the following lines to your file

mkcd () {
mkdir -p "$*"
cd "$*"

3. Try it out

~$ mkcd dir

Original post here: