This script takes two (optional) arguments, the directory to partition, and the parition size. Since you didn't say whether you only want to move files, or move everything, I assumed you meant files, so I used the find command.
A few comments,
- Had you not specified shell, something like this is more easily done in perl, ruby, or python.
- find with maxdepth 1 does only the directory
- you can move the files anywhere, just change the folder naming
- since find is used, you can add -name, -mtime, -ctime, etc.
Copysome.sh,
#!/bin/bash
path=${1:-"."} #directory to start
howmany=${2:-20} #partition size
pushd $path; #move there
part=1; #starting partition
LIST="/usr/bin/find -maxdepth 1 -type f" #move only files?
#LIST="ls" #move everything #be careful, $folder will get moved also :-)
count=`$LIST |/usr/bin/wc -l`; #count of files to move
while [ $count -gt 0 ]; do
folder="folder-$part";
if [ ! -d $folder ]; then /usr/bin/mkdir -p $folder; fi
/usr/bin/mv `$LIST |/usr/bin/sort |/usr/bin/head -$howmany` $folder/.
count=`$LIST |/usr/bin/wc -l`; #are there more files?
part=$(expr $part + 1)
done
popd $path
Here is a script to test with (I didn't have an extra 1000 files lying around),
for f in 0 1 2 3 4 5 6 7 8 9; do
for g in 0 1 2 3 4 5 6 7 8 9; do
for h in 0 1 2 3 4 5 6 7 8 9; do
touch $f$g$h
done
done
done