This is something I do frequently
$ mkdir foo
$ cd foo
This works as a single command, but it's more keystrokes and saves no time.
$ mkdir foo && cd foo
Is there a shortcut for this?
Edit
With the use of help below, this seems to be the most elegant answer.
# ~/.bashrc
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "\`$1' already exists"
else
mkdir $1 && cd $1
fi
}
mkdir
if you usecommand mkdir $1
instead of justmkdir $1
in the function body.mkdir -p $1; cd $1
so you can make nested directories and move into them. @quackquixote