13

I have a directory which contain more than thousand images. I want to split the files in to multiple sub directories, each containing 100 files. How do I do this?

1
31

the following worked well for me. Open the directory in terminal, and just copy & paste the following script, press enter. sub directories will be created inside having names dir_001, dir_002 and so on.

i=0; 
for f in *; 
do 
    d=dir_$(printf %03d $((i/100+1))); 
    mkdir -p $d; 
    mv "$f" $d; 
    let i++; 
done
2
  • This was flagged as low quality. Since you are the OP, could you add an explanation of what you have done? – muru Feb 12 '15 at 16:16
  • 1
    done. Have added an explanation. – Aneeez Feb 12 '15 at 18:45
1

Move fixed number of files using array with range and offset.

#!/bin/bash

shopt -s nullglob

a=(./src/*)
for ((i=0; i<${#a[@]}; i+=100)); do
    printf -v b ./img_%03d $((++n))
    mkdir -p $b && mv -t $b "${a[@]:$i:100}"
done
0

This will prompt you with how many directories you want and the sub-directory prefix. The ls command is just a reminder of how many files you've got.

ls -1 | wc -l

read -p 'How Many Directories: ' F;
read -p 'Sub-Directories Prefix: ' S;

PARRENT=${PWD}
# cd $PARRENT 
n=0
for i in *
do
  if [ $((n+=1)) -gt $F ]; then
    n=1
  fi
  todir=$PARRENT/"$S"_$n
  [ -d "$todir" ] || mkdir "$todir" 
  mv "$i" "$todir" 
done

Source: https://www.unix.com/shell-programming-and-scripting/248539-split-folder-huge-number-files-n-folders.html

1
  • I'm afraid this will cause problems if current directory have spaces in path, but I might be wrong. – val is still with Monica Feb 11 '20 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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