from the comments here: https://generateme.wordpress.com/2015/08/12/training-own-dnn-for-deep-dream/

I have around 15k images and each one needs its own folder. I ran this once, when I had fewer images, and it worked fine:

a=0; for i in *.jpg; do mkdir -p $a; mv $i $a; a=$((a+1)); done

But not when I run it, 10000.jpg goes into folder "0". I figured it had something to do with the 5th digit but I scaled back to 9999 and it put 1000.jpg into "0". I check further in on a random one like 1934 and by then it's completely fallen apart and I have no idea why that particular .jpg number is in that numbered folder. Is there a way to fix this line or is there another line I should be using to do this?

  • You may or may not have whitespace in your filenames, but you should learn to quote your variables regardless.
    – Wildcard
    Commented Mar 29, 2016 at 19:19
  • are you sure you want each file in its own separate folder? what's the advantage of having 15K subdirectories versus just having 15K files in the one folder? perhaps what you need is some hashing method for allocating particular files to one of a dozen or a hundred (or however many) folders, so that you avoid the slowdown experienced with some filesystems (but not all) when they have thousands of files/dirs in a single folder.
    – cas
    Commented Mar 30, 2016 at 2:39

1 Answer 1


You don't explicitly seem to say so, but if your all your JPEG files have filenames than the easier way to do this is to use ${x%.jpg} to make a variable that is just the base name of the JPEG file without .jpg:

for i in *.jpg; do d="${i%.jpg}"; mkdir "$d"; mv "$i" "$d"; done
  • I just change the extension to zip and it worked like charm, thank you my hero
    – DaWe
    Commented Jun 18 at 12:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .