Skip to content

bash - enhanced mkdir

Since i am fully addicted to the shell, the customisation started.

This time, i want to share a small enhancement for the "mkdir" bash command. The code i will paste below is doing the following thing. If you supply one argument, it will create the directory you want with "mkdir -p" and change into that. If you supply multiple arguments, it will behave like the normal "mkdir".

How to use it? Open your .bashrc file and add the code provided below. Then define an alias like "alias mkdir=netbazzlinemkdir". When you open your next shell, your mkdir is enhanced :-).

function net_bazzline_mkdir ()
  #check if at least one argument is supplied
  if [ $# -eq 0 ]
    echo "No arguments supplied"
    return 1

  #if one argument is supplied, create dir and
  # change to it
  if [ $# -eq 1 ]
    mkdir -p "$1"
    cd "$1"
    return 0

  #if more then one argument is supplied
  # execute default mkdir
  if [ $# -gt 1 ]
    mkdir $@
    return 0

Sourceode available on

Translate to de es fr it pt ja