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
}
mkdirif you usecommand mkdir $1instead of justmkdir $1in the function body. – Andy Jun 15 '10 at 14:50mkdir -p $1; cd $1so you can make nested directories and move into them. @quackquixote – yomotherboard Oct 06 '21 at 03:58