Todd Jenk
Todd Jenk

Reputation: 311

How can I split a folder with thousands of images into multiple sub-folders?

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.

Upvotes: 21

Views: 11770

Answers (2)

Lri
Lri

Reputation: 27613

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

Upvotes: 45

anubhava
anubhava

Reputation: 785058

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
}'

Upvotes: 4

Related Questions