Search code examples
bashpipemkdir

Pipe a number into mkdir


I am trying to make a directory for every file in a directory. So far I have:

ls *output | sort -V | wc -l | xargs -I[] mkdir Cele_{1..[]}

But this results in just 1 directory named Cele_{1..1000} instead of making 1000 directories.

I am confused since typing mkdir Cele_{1..1000} creates 1000 directories, not one directory named Cele_{1..1000}.

What am I missing here?


Solution

  • the last command with xargs needs to be evaluated

     x=$(ls * | sort -V | wc -l  | xargs  -I[] echo Cele_{1..[]})
     eval mkdir $x
    

    more error phrone approach (one liner)

    shopt -s nullglob;fl=(*); eval mkdir Cele_{1..${#fl[@]}}; shopt -u nullglob;
    

    with multiple lines

    shopt -s nullglob
    fl=(*)
    eval mkdir Cele_{1..${#fl[@]}}
    shopt -u nullglob