the function
This little mkcd function feels like it should be a standard /bin tool. It makes a directory and changes directory into it with a couple of safeties.
# create and cd into a new directory (safely)
# Usage: mkcd <directory-name>
mkcd() {
if [[ "$1" == *\** || "$1" == *\?* || "$1" == *\[*\]* ]]; then
echo "❌ Globs not supported—please provide a single directory name."
return 1
fi
if [[ $# -gt 1 ]]; then
echo "⚠️ Multiple directory names given—using the first: $1"
fi
if [[ -z "$1" ]]; then
echo "📁 Usage: mkcd <directory-name>"
return 1
fi
mkdir -p "$1" && cd "$1" || {
echo "❌ Failed to enter directory: $1"
return 1
}
}
usage
mkcd folder-to-create
mkcd folder/subfolder/subfolder2
Just add to your startup files (e.g., .bashrc
, .zshrc
) and reload; you’ll be set.