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
.
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.
"$_"
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 intosed
to replacemkdir
withmcd
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, cd
ing into the last created one.
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
Suggestion: please add the
-p
option to mkdir:mcd() { mkdir -p "$@" 2> >(sed s/mkdir/mcd/ 1>&2) && cd "$_"; }
That way,
mcd foo/bar/baz
will work even if foo or foo/bar aren't present yet.