I have a folder that has 200.000 images. I want to do the following:

  1. Move an X amount of images (50 images, 100 images, 200 images) from that folder to Y folder.
  2. Each Y folder can be a number (Folder 1, Folder 2, Folder 3...).
  3. The end result might look like this:

Folder 1 (Has X amount of images) Folder 2 (Has X amount of images) Folder 3 (Has X amount of images) Folder 4 (Has X amount of images)


Run the script in your image directory. Make sure that destination doesn't have any sub directories.

IMG=100     # Number of images to move
DES="."     # Destination

I=$(find . -maxdepth 1 -mindepth 1 -type f | wc -l)
N=$(( (I / IMG) + (I % IMG) ))
eval mkdir "$DES"/Folder{01..$N}

for i in $(find "$DES" -maxdepth 1 -mindepth 1 -type d | sort); do
    mv $(find . -maxdepth 1 -mindepth 1 -type f | sort | grep -m $IMG '.*') $i
  • Hi Basharat, the script is good but is not working. It throws the error "line 9: /bin/ls: Argument list too long" because of the 200K images. – Luis Alvarado Oct 17 '12 at 19:17
  • @LuisAlvarado fixed. – Basharat Sialvi Oct 18 '12 at 7:24

Try this script, it should fulfil your request but the last X files will remain in the original directory


while [ `find $DIR -OrigDIR 1 -type f| wc -l` -gt $NumberOfMovedItems ] ; do
  mkdir $NewSubDir
  find $OrigDIR -maxdepth 1 -type f | head -n $NumberOfMovedItems | xargs -I {} mv {} $NewSubDir
  let Count++

Change the path of FolderX and FolderY as you like.

When you run the script you have to provide number of items to move and name of Subdirectory


  • The script as it is, is not working. Already added the $1 and $2. Tested the script you mentioned in the link and that one is working. If you can, please edit your answer to correct the the script. – Luis Alvarado Oct 17 '12 at 19:17

I would start with creating a list

ls X > X.list
for Y in `seq 1 10` ; do mkdir $Y ; mv `head -n $(( Y * 50 )) X.list | tail -n 50` $Y ; done

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.