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=net_bazzline_mkdir". 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


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Markdown format allowed
Form options