if [ "x$selected" != "x" ]; then
if [ "$first" == "$last" ]; then
for j in $selected; do
j2=`echo "$j" | tr "[:upper:]" "[:lower:]"`
for k in $j* $j2*; do
if [ "$k" != "$dir" -a -f "$k" ]; then
mv "$k" "$dir/"
initials=`for i in *; do echo "$i" | tr "[:lower:]" "[:upper:]" | sed "s/^\(.\).*/\1/"; done | sort | uniq`;
for i in $initials; do
if [ "$first" == "" ]; then
numfiles=`ls|grep -i "^$i\+"|wc -l`;
(( cur = cur + $numfiles ));
if [ $cur -ge $limit ]; then
It splits a directory into smaller directories based on the first character in the filename. An example:
$ for i in `seq 1 1000`; do touch `pwgen -n -c 10 1`; done
A B-C D-E F-G H-I J-K L-M N-O P Q-S T U V-W X-Y Z
As you see, it splits into ranges when it can fit few enough files into one directory.
$limit as defined at the top of the script controls the number of files that should be exceeded for the script to deem it necessary to create another directory; that is, $limit is not a hard limit, there is no hard limit, and there can't be, unless you start splitting on more than the first character. In any case, adjust as necessary.
The algorithm currently used is greedy and likely quite non-optimal, but it works for me.
The script was made to be able to split directories with many files on the CF card of my NES PowerPak (which is a fabuluous creation, but that's another story).