Originally Posted by
wyattwhiteeagle
I have over 600 mp3 audio files.
In scripting, is there a way to sort these files into separate subdirectories while retaining a max size limit for the subdrectories?
Each subdirectory can hold no more than 100 megabytes.
(avoiding using "current working directory")
Here you go. No warranty - use at your own risk, as I made in in 10 minutes on the morning toilet for entertainment purposes only.
Code:
#!/bin/bash
source_dir=mp3s
target_dir=music
size_limit=100000 # 100 Mb
n=0
while find $source_dir -mindepth 1 -maxdepth 1 | read ; do
target="$target_dir-$n"
if [[ ! -d $target ]] ; then mkdir $target ; fi
while [[ $(du $target | awk '{print $1}') -lt $size_limit ]] ; do
if find $source_dir -mindepth 1 -maxdepth 1 | \
grep ".mp3" >/dev/null; then
find $source_dir -type f -name "*.mp3" \
-exec mv {} $target \; -quit
else
break
fi
sleep 1
done
n=$((n + 1))
done
Bookmarks