Question: I have a 1000s of files in a directory with and I want to be able to divide them into sub-directories, with each sub-directory containing a specific number of files. I don't care what files go into what directories, just as long as each contain a specific number. All the file names have a common ending (e.g. .txt) but what goes before varies. 

Anyone know an easy way to do this.

Answer: 
Assuming you only have files ending in `*.txt`, no hidden files and no directories:

```
#!/bin/bash
shopt -s nullglob

maxf=42

files=( *.txt )

for ((i=0;maxf*i<${#files[@]};++i)); do
    s=subdir$i
    mkdir -p "$s"
    mv -t "$s" -- "${files[@]:i*maxf:maxf}"
done

```

This will create directories `subdirX` with `X` an integer starting from 0, and will put 42 files in each directory.

You can tweak the thing to have padded zeroes for `X`:

```
#!/bin/bash

shopt -s nullglob

files=( *.txt )
maxf=42
((l=${#files[@]}/maxf))
p=${#l}

for ((i=0;maxf*i<${#files[@]};++i)); do
    printf -v s "subdir%0${p}d" "$i"
    mkdir -p "$s"
    mv -t "$s" -- "${files[@]:i*maxf:maxf}"
done

```
How about 

```
find *.txt -print0 | xargs -0 -n 100 | xargs -I {} echo cp {} '$(md5sum <<< "{}")' | sh

```

This will create several directories each containing 100 files. The name of each created directory is a md5 hash of the filenames it contains.