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 ]
  then
    echo "No arguments supplied"
    return 1
  fi

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

  #if more then one argument is supplied
  # execute default mkdir
  if [ $# -gt 1 ]
  then
    mkdir $@
    return 0
  fi
}
Sourceode available on github.com.

Trackbacks

Keine Trackbacks

Kommentare

Ansicht der Kommentare: Linear | Verschachtelt

Noch keine Kommentare

Kommentar schreiben

Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.
Um einen Kommentar hinterlassen zu können, erhalten Sie nach dem Kommentieren eine E-Mail mit Aktivierungslink an ihre angegebene Adresse.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA

Markdown-Formatierung erlaubt
Formular-Optionen