I have a large amount of files which I am trying to organize into three folders alphabetically. I'm trying to get a bash script together which has the ability to get the first letter of the file, then move it into a folder based on that first letter.
For example:
file -> folder name
apples -> A-G
banana -> A-G
tomato -> H-T
zebra -> U-Z
Any tips would be appreciated! TIA!
#!/bin/bash
dirs=(A-G H-T U-Z)
shopt -s nocasematch
for file in *
do
for dir in "${dirs[@]}"
do
if [[ $file =~ ^[$dir] ]]
then
mv "$file" "$dir"
break
fi
done
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With