 <p>This is probably slower than a Perl program (1 minute for 10.000 files) but it should work with any POSIX compliant shell.</p> <pre><code>#! /bin/sh nd=0 nf=0 /bin/ls | \ while read file; do case $(expr $nf % 10) in 0) nd=$(/usr/bin/expr $nd + 1) dir=$(printf "dir_%04d" $nd) mkdir $dir ;; esac mv "$file" "$dir/$file" nf=$(/usr/bin/expr $nf + 1) </code></pre> <p>done</p> <p>With bash, you can use arithmetic expansion $((...)).</p> <p>And of course this idea can be improved by using xargs - should not take longer than ~ 45 sec for 2.5 million files.</p> <pre><code>nd=0 ls | xargs -L 1000 echo | \ while read cmd; do nd=$((nd+1)) dir=$(printf "dir_%04d" $nd) mkdir $dir mv $cmd $dir done </code></pre>