Creating numerous directories using mkdir

  • One

    for i in {1..50}; do
      mkdir s"$i"
    done
    
  • Two

    mkdir s{1..50}
    

    This option works in bash, zsh and ksh93

  • Three

    mkdir $(printf "s%02i " $(seq 1 50))
    

You can do this with a shell script.

Pure sh - this will work even on pre-POSIX bourne shells:

n=1;
max=50;
while [ "$n" -le "$max" ]; do
  mkdir "s$n"
  n=`expr "$n" + 1`;
done

If you want to create a high number of directories, you can make the script faster by reducing it to a single call of mkdir as well as using shell builtins for testing and arithmetics. Like this:

n=1
max=50
set -- # this sets $@ [the argv array] to an empty list.

while [ "$n" -le "$max" ]; do
    set -- "$@" "s$n" # this adds s$n to the end of $@
    n=$(( $n + 1 ));
done 

mkdir "$@"

Zsh, ksh93 or bash make this much easier, but I should point out this is not built into mkdir and may not work in other shells. For larger cases, it may also be affected by limits on the number or total size of arguments that may be passed to a command.

mkdir s{1..50}

Lots of complicated answers here, but bash makes it really easy. Sure, the pure POSIX solution works, but why not take advantage of the bash shell you're using, anyhow? You can do this easily with brace expansion:

% mkdir -v s{1..10} && ls -1d s{1..10}                                   (09-24 17:37)
mkdir: created directory `s1'
mkdir: created directory `s2'
mkdir: created directory `s3'
mkdir: created directory `s4'
mkdir: created directory `s5'
mkdir: created directory `s6'
mkdir: created directory `s7'
mkdir: created directory `s8'
mkdir: created directory `s9'
mkdir: created directory `s10'
s1
s10
s2
s3
s4
s5
s6
s7
s8
s9