Moving files to subdirectories
March 7, 2020
| By Haluk Dogan
If you have a directory with lots of files in it, this POSIX compliant Bash script is here to help you to move its files into subdirectories.
#!/usr/bin/env bash
echo "$(find . -type f | wc -l)" files
printf "How many subdirectories: "
read -r F
printf "Subdirectories prefix: "
read -r S
PARENT=${PWD}
n=0
for i in *
do
if [ $((n+=1)) -gt "$F" ]; then
n=1
fi
todir=$PARENT/"$S"_$n
[ -d "$todir" ] || mkdir "$todir"
mv "$i" "$todir"
done