Skip to content

Instantly share code, notes, and snippets.

@falzm
Created March 3, 2021 08:51
Show Gist options
  • Save falzm/ec22dd347eeb10336ea35c0ae563abbc to your computer and use it in GitHub Desktop.
Save falzm/ec22dd347eeb10336ea35c0ae563abbc to your computer and use it in GitHub Desktop.
Simple Bash function to create a directory and cd to it immediately. Creates a temporary directory by passing flag "-t".
mkcd() {
local dir=""
while getopts "ht" c
do
case $c in
t) dir=$(mktemp -d) ;;
h|?) echo "mkcd [-t] [DIRECTORY]" ; return ;;
esac
done
shift $((OPTIND-1))
[[ -n "$dir" ]] || dir="$1"
[[ -d "$dir" ]] || mkdir "$dir"
cd "$dir"
OPTIND=$#
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment