There's no built-in function for that, but you can use shell functionality to help you not have to type the argument of the cd
command again after running mkdir
:
- Type
cd
, then Esc . (or Alt+.) to insert the last argument from the previous command.
cd !$
executes cd
on the last argument of the previous command.
- Press Up to recall the previous command line, then edit it to change
mkdir
into cd
.
You can define a simple make-and-change-directory function in your ~/.bashrc
:
mkcd () { mkdir "$1" && cd "$1"; }
Reload your .bashrc
(. ~/.bashrc
) or restart bash, and now you can type mkcd new-directory
.
This simple version fails in some unusual cases involving weird directory names or ..
and symbolic links. Here's one that does. For explanations, see the Unix & Linux version of this question.
mkcd () {
case "$1" in
/*) mkdir -p "$1" && cd "$1";;
*/../*) (cd "./${1%/../*}/.." && mkdir -p "./${1##*/../}") && cd "$1";;
../*) (cd .. && mkdir -p "${1#.}") && cd "$1";;
*) mkdir -p "./$1" && cd "./$1";;
esac
}