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 ]
  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.

Translate to de es fr it pt ja

bash - unrar more than one file in separate directories

Long story short, here is the script.

#!/bin/sh for f in \*.rar do mkdir ${f%.rar} unrar e $f ${f%.rar}/ done
What does it? For every *.rar file in you current directory, a subdirectory will be created by using the filename except of '.rar'.

Just use this script on your command line.

sh ~/my/path/to/the/script.sh
Thats it, have fun :-).

It should not be that problem to adapt this script to other types of archive.

Want to know more about bash scripting and string manipulation? Try to check the following links. string manipulation special parameters

Translate to de es fr it pt ja