Linux-Tech-Tips/Scripts/sort-files.sh

40 lines
997 B
Bash
Raw Permalink Normal View History

2024-11-01 13:16:16 +01:00
#!/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