Last active
November 11, 2024 11:49
-
-
Save tiagosiebler/f64019e1a91cea05be7c4fb3ebb00727 to your computer and use it in GitHub Desktop.
Sorts a huge folder into sub folders, by file type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varName=./_sorted | |
if [ ! -d "$varName" ]; then | |
echo "Creating $varName"; | |
mkdir "$varName"; | |
fi | |
## array variable containing extensions | |
declare -a extensions=("dmg" "archives" "pdfs" "images" "php" "html" "js" "documents" "powerpoints" "excel" "java" "logs" "objc" "emails" "videos" "dbbackup") | |
declare -a images=("jpg" "JPG" "jpeg" "png" "PNG" "gif" "jpg" "svg" "webp") | |
declare -a archives=("zip" "gz" "rar" "7z" "bz2" "tar" "tgz" "hqx") | |
declare -a documents=("doc" "docx" "txt" "rtf" "xml" "md" "PDF" "yaml" "yml") | |
declare -a logs=("log") | |
declare -a powerpoints=("ppt" "pptx") | |
declare -a excel=("xls" "xlsx" "csv" "json" "XLSX" "xlsm") | |
declare -a videos=("mp4" "webm" "mov" "wmv" "mkv") | |
declare -a dbbackup=("sql" "sql.gz") | |
## now loop through the above array | |
for i in "${extensions[@]}" | |
do | |
# check if subdir exists | |
subdir="$varName/$i"; | |
echo "=> Checking $subdir"; | |
# create subdir if not | |
if [ ! -d "$subdir" ]; then | |
echo "Creating $subdir"; | |
mkdir "$subdir"; | |
fi | |
# move all matching files | |
case "$i" in | |
"dmg") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.$i "$subdir/"; | |
mv -- *.pkg "$subdir/"; | |
mv -- *.mpkg "$subdir/"; | |
;; | |
"php") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.$i "$subdir"; | |
;; | |
"html") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.$i "$subdir"; | |
mv -- *.htm "$subdir"; | |
mv -- *.HTML "$subdir"; | |
;; | |
"js") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.$i "$subdir"; | |
mv -- *.ts "$subdir"; | |
;; | |
"java") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.$i "$subdir"; | |
mv -- *.jar "$subdir"; | |
;; | |
"objc") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.m "$subdir"; | |
mv -- *.h "$subdir"; | |
mv -- *.mm "$subdir"; | |
mv -- *.plist "$subdir"; | |
mv -- *.cer "$subdir"; | |
mv -- *.ini "$subdir"; | |
mv -- *.mobileprovision "$subdir"; | |
;; | |
"pdfs") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.pdf "$subdir"; | |
;; | |
"emails") | |
echo "==> Moving $i to $subdir"; | |
mv -- *.msg "$subdir"; | |
mv -- *.eml "$subdir"; | |
;; | |
"archives") | |
echo "==> Processing archives"; | |
for y in "${archives[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"images") | |
echo "==> Processing images"; | |
for y in "${images[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"documents") | |
echo "==> Processing documents"; | |
for y in "${documents[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"powerpoints") | |
echo "==> Processing powerpoints"; | |
for y in "${powerpoints[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"excel") | |
echo "==> Processing excel"; | |
for y in "${excel[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"logs") | |
echo "==> Processing logs"; | |
for y in "${logs[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"videos") | |
echo "==> Processing videos"; | |
for y in "${videos[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
"dbbackup") | |
echo "==> Processing dbbackup"; | |
for y in "${dbbackup[@]}" | |
do | |
echo "===> Processing ext: $y"; | |
mv -- *.$y "$subdir"; | |
done | |
;; | |
# default handler: | |
*) | |
echo "Unhandled type $i"; | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment