How can I split a folder with thousands of images into several subfolders?

I have a directory with approximately 5,000 images, and I would like to split / move it into 50 folders (which will need to be created) with 100 images each.

Is there a way to do this using a terminal?

I am running OS X.

+8
command-line bash macos
source share
2 answers

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

+25
source share

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

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

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

Also move files to 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 }' 
+2
source share

All Articles