Skip to content

Instantly share code, notes, and snippets.

@steezeburger
Last active October 6, 2023 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steezeburger/3bec45078c4a67c685824ec977a3bfec to your computer and use it in GitHub Desktop.
Save steezeburger/3bec45078c4a67c685824ec977a3bfec to your computer and use it in GitHub Desktop.
Organize your downloads folder by file type
#!/bin/bash
set -e
set -o pipefail
# Navigate to Downloads directory or exit with status 1 if it fails
cd ~/Downloads || exit 1
# Indexed array for directory names
directories=("3D_Models" "Archives" "Audio" "Books_and_Presentations" "Documents" "Images" "Software" "Videos")
# Corresponding indexed array for file extensions
# Each line correlates with the directory at the same index in the `directories` array
extensions=(
"fbx obj stl blend glb 3ds" # 3D_Models
"zip ZIP 7z 7Z gz GZ" # Archives
"mid MID mp3 MP3 wav WAV" # Audio
"pdf PDF azw3 AZW3 epub EPUB pptx PPTX docx DOCX doc DOC odt ODT rtf RTF onepkg ONEPKG" # Books_and_Presentations
"txt TXT csv CSV tsv TSV json JSON xml XML md MD key KEY" # Documents
"jpeg JPEG jpg JPG png PNG webp WEBP gif GIF svg SVG ico ICO" # Images
"iso ISO dmg DMG exe EXE app APP apk APK sh SH bin BIN pkg PKG deb DEB rpm RPM" # Software
"3gpp mp4 MP4 mov MOV avi AVI flv FLV wmv WMV mkv MKV webm WEBM ogv OGV" # Videos
)
# Move files to their respective directories
move_files() {
local extension="$1"
local target_dir="$2"
find . -maxdepth 1 -type f -name "*.$extension" -exec mv {} "$target_dir"/ \; 2>&1 | while read -r line; do
if [[ "$line" != *"No such file or directory"* ]]; then
echo "Error moving *.$extension files: $line"
fi
done
}
# Create directories and move files to their respective directories
for i in "${!directories[@]}"; do
dir="${directories[$i]}"
echo "Creating directory if not exists: $dir"
mkdir -p "$dir"
exts="${extensions[$i]}"
for ext in $exts; do
echo "Moving *.$ext files to $dir"
move_files "$ext" "$dir"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment