From f3a07239946705d913eccda756c4e3de2e4c631c Mon Sep 17 00:00:00 2001 From: dataprolet Date: Fri, 1 Nov 2024 13:16:16 +0100 Subject: [PATCH] Add Scripts/sort-files.sh --- Scripts/sort-files.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Scripts/sort-files.sh diff --git a/Scripts/sort-files.sh b/Scripts/sort-files.sh new file mode 100644 index 0000000..3539859 --- /dev/null +++ b/Scripts/sort-files.sh @@ -0,0 +1,40 @@ +#!/bin/usr/env bash + +## Run this script in any directory that's filled with random files +## you want to sort by file extensions into respective subdiorectories. + +# Change to current directory +cd $(pwd) + +# Create subdirectories +dest="_sortiert" +mkdir -p "${dest}" + +dirs=("Images" "Archives" "Videos" "Audio" "Documents" "Misc") +for dir in "${dirs[@]}"; do + mkdir -p "${dest}/${dir}" +done + +# Function for moving files +move() { + local dir="$1" + shift + local extensions=("$@") + + for ext in "${extensions[@]}"; do + mv ./*."${ext}" "${dest}/${dir}" 2>/dev/null + done +} + +# Move files by extension +move "Images" "jpg" "jpeg" "JPG" "png" "PNG" "svg" "gif" "webp" "bmp" +move "Archives" "zip" "rar" "tar" "gz" +move "Videos" "MOV" "mov" "mp4" "mkv" "avi" +move "Audio" "mp3" "ogg" "mpeg" "wav" "flac" +move "Documents" "doc*" "odt" "pdf" "txt" "ods" + +# Move misc +find . -maxdepth 1 -type f -exec mv {} "${dest}/Misc" \; + +# Remove empty directories +find "${dest}" -type d -empty -delete \ No newline at end of file