20

I have a directory with about 5000 images and I'd like to split/move it in to 50 folders (which will need to be created) with 100 images each.

Is there a way to do this using terminal?

I'm running OS X.

Antarus
  • 1,573
  • 2
  • 15
  • 32
Todd Jenk
  • 301
  • 1
  • 3
  • 13

2 Answers2

45

i=0; for f in *; do d=dir_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done

Lri
  • 26,768
  • 8
  • 84
  • 82
4

awk one-liner can do that. Consider this awk command:

find . -name "*.JPG" | awk '!(++cnt%100) {"mkdir sub_" ++d|getline}'

Run it inside the folder with 5000 images. This will create 50 folders with the names sub_1, sub_2...sub_50.

Also to move files into these newly created directories:

find . -type f | awk '{
   a[++cnt] = $0
}
cnt==100 {
   subd = "sub_" ++d;
   system("mkdir " subd);
   for (f in a)
      system("mv " a[f] " " subd);
   cnt=0
}'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks but I'm getting the errors find: -name: requires additional arguments – Todd Jenk Jul 16 '13 at 07:22
  • Thanks for continuing to help but it seems to not be selecting the correct name. I am seeing this *** error mv: rename ./2012-09-10 to sub_2/2012-09-10: No such file or directory *** the filenames are IMG_2789.JPG for example – Todd Jenk Jul 16 '13 at 10:57
  • Do you have all the images with `.JPG` extension? – anubhava Jul 16 '13 at 10:58