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?
3 Answers
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
-
This was flagged as low quality. Since you are the OP, could you add an explanation of what you have done?– muruFeb 12, 2015 at 16:16
-
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
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
-
I'm afraid this will cause problems if current directory have spaces in path, but I might be wrong. Feb 11, 2020 at 18:11