> mkdir newdirBut wouldn't this be more easily done in one single command? After all, the same directory name is used in each command.
> cd newdir
I created a bash shell functiondetails-1 that does exactly this. I called it "mkcd", since it is basically a combination of "mkdir" and "cd". It is defined as
mkcd () {This would be defined in a .bashrc environment initialization file.
if [ $# == 1 ]; then
dir="$1"
printf "mkcd %s\n" "${dir}"
if [ -d "${dir}" ]; then
cd "${dir}"
else
mkdir -p ${dir}
if [ $? == 0 ]; then
cd "${dir}"
fi
fi
else
printf "Usage: mkcd <dir>\n"
fi
}
This will skip the directory creation if it already exists. It actually uses "mkdir -p" so that it also creates any intermediate directory levels needed. It also only performs the "cd" if directory creation had no errors.
The operation can now be done as
> mkcd newdir
Notes