91
27
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
}
1You can rename the function to
mkdir
if you usecommand mkdir $1
instead of justmkdir $1
in the function body. – Andy – 2010-06-15T14:50:50.3032(1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif"). – quack quixote – 2010-06-15T16:25:43.017