Search code examples
bashmkdirxargs

How to use mkdir with xargs


i am new to xargs..

i want to use mkdir with xargs to create some folders similar to:

for i in {1..11};do mkdir mm$i;done

which will create folders mm1, mm2, mm2 ... mm11

i tried:

echo {1..11}|xargs -p -n1 -I {} mkdir -p "mm{}"

however it prompt:

mkdir -p mm1 2 3 4 5 6 7 8 9 10 11 ?...

i also tried:

echo {1..11}|xargs -p -n1 mkdir -p mm

it prompt:

mkdir -p mm 1 ?...n
mkdir -p mm 2 ?...n
mkdir -p mm 3 ?...n
mkdir -p mm 4 ?...n
mkdir -p mm 5 ?...n
mkdir -p mm 6 ?...n
mkdir -p mm 7 ?...n
mkdir -p mm 8 ?...n
mkdir -p mm 9 ?...n
mkdir -p mm 10 ?...n
mkdir -p mm 11 ?...n
mkdir -p mm ?...n

help.. tks


Solution

  • Your problem is that you are using the -I flag, which requires that arguments be separated by blank lines rather than whitespace:

    -I replace-str
           Replace occurrences of replace-str in the initial-arguments with
           names  read  from  standard input.  Also, unquoted blanks do not
           terminate input items; instead  the  separator  is  the  newline
           character.  Implies -x and -L 1.
    

    One solution is to generate appropriate input:

    echo {1..11} | tr ' ' '\n' | xargs ...
    

    Or:

    seq 11 | xargs ...
    

    Or, as @gniourf_gniourf suggests in the comment, just use shell expansion directly for this particular case.