Skip to content

Instantly share code, notes, and snippets.

@kallmanation
Last active Jan 15, 2021
Embed
What would you like to do?
mcd - mkdir && cd

mcd

Make and Change Directory

mkdir && cd

It's an often used pattern when setting up to first make a directory with mkdir and then immediately moving into the new directory with cd.

mkdir my_dir
cd my_dir

It's somewhat useful to combine these commands into one, mcd.

The code

mcd() { mkdir "$@" 2> >(sed s/mkdir/mcd/ 1>&2) && cd "$_"; }

Simply place the above in .bashrc or similar, or make a copy of mcd.sh for a portable executable.

Explanation

  • "$_" refers to the last argument of the previously executed command.
  • && executes the subsequent command iff the preceding command succeeds.
  • "$@" refers to all the arguments given to this command.
  • 2> >(sed s/mkdir/mcd/ 1>&2) redirects stderr into sed to replace mkdir with mcd and then redirects back onto stderr. This gives slightly more coherent error messages, but is not strictly necessary for the functionality.

Putting it together: we call mkdir with all the same arguments as passed to us; if we successfully create the directory, we call cd with the last parameter (aka the directory we just made).

Using "$@" instead of "$1" or similar has some benefits: mcd will respect any parameters passed, such as -p; it will also maintain the mkdir capability of creating multiple directories at once, cding into the last created one.

Examples

Starting from an empty pre-existing directory ~/mcd/

~/mcd $ mcd
usage: mcd [-pv] [-m mode] directory ...
~/mcd $ mcd A/B
mcd: A: No such file or directory
~/mcd $ mcd -p A/B
~/mcd/A/B $ cd -
~/mcd $ mcd A/B/C
~/mcd/A/B/C $ cd -
~/mcd $ mcd A
mcd: A: File exists
~/mcd $ mcd -p A
~/mcd/A $ mcd X Y Z
~/mcd/Z $ cd -
~/mcd/A $ ls
B X Y Z
mcd() { mkdir "$@" 2> >(sed s/mkdir/mcd/ 1>&2) && cd "$_"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment