5

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)

0

4 Answers 4

4

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
done
2
  • 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. Oct 17, 2012 at 19:17
  • @LuisAlvarado fixed. Oct 18, 2012 at 7:24
1

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

OrigDIR=~/FolderX
DestDIR=~/FolderY
NumberOfMovedItems=$1
SubDestDir=$2
Count=1

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

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

Source

1
  • 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. Oct 17, 2012 at 19:17
0

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
0

About @BasharatSialvi answer (respectfully):
It works but it has two problems:
1- It creates I % IMG extra directories which will remain empty
2- It has a classic bash mistake commonly known as bash pitfall number 1.
The first problem is not that critical as you can delete those extra directories, but the second problem makes everything goes wrong in many cases. For example when you have space in your file names.

So I enhance @BasharatSialvi 's answer:

COUNT=5             # Number of files to move
DES="."             # Destination
DESFOLPRE="folder"  # Prefix of the destination folders

I=$(find . -maxdepth 1 -mindepth 1 -type f | wc -l)  # Find all files
N=$((I / COUNT))                                     # Number of directories needed 
if [[ $((I % COUNT)) != 0 ]]; then N=$((N+1)); fi    # Add one directory if cant fit files perfectly
eval mkdir "$DES"/"$DESFOLPRE"{01..$N}               # Create directories

for i in $(eval echo {01..$N})                       # For every directory move COUNT files
do
  $(find . -maxdepth 1 -mindepth 1 -type f | sort | head -$COUNT | xargs -I filename mv filename $DES/$DESFOLPRE$i/)
done

Notice that neither Basharat's answer nor mine answer don't care about file types, so saying that they move only images is wrong. They move all files.

Hope it helps.

You must log in to answer this question.

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