Created
August 8, 2025 16:36
-
-
Save OdinsHat/78b8355184797bf9382c0eda029d6344 to your computer and use it in GitHub Desktop.
Sort Linux home Downloads directory into appropriate folders
This file contains hidden or 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
| #!/bin/bash | |
| # ============================================================================== | |
| # This script organizes files in my Downloads directory by file type. | |
| # It creates subdirectories for common file types (e.g., PDFs, Images) and | |
| # moves the files into the appropriate folders. | |
| # Assumes a Downloads directory at /home/USER/Downloads | |
| # ============================================================================== | |
| # Define the Downloads directory. | |
| DOWNLOADS_DIR="$HOME/Downloads" | |
| # Check if the Downloads directory exists. If not, exit with an error. | |
| if [ ! -d "$DOWNLOADS_DIR" ]; then | |
| echo "Error: The downloads directory was not found at $DOWNLOADS_DIR." | |
| exit 1 | |
| fi | |
| echo "Starting the organization of files in: $DOWNLOADS_DIR" | |
| echo "-----------------------------------------------------" | |
| # Declare an associative array to map file extensions to directory names. | |
| # This makes the script easy to modify and extend. | |
| declare -A file_types | |
| file_types=( | |
| # Documents | |
| [pdf]="PDFs" | |
| [txt]="Documents" | |
| [doc]="Documents" | |
| [docx]="Documents" | |
| [xls]="Documents" | |
| [xlsx]="Documents" | |
| [ppt]="Documents" | |
| [pptx]="Documents" | |
| [ods]="Documents" | |
| [csv]="Documents" | |
| #Ebooks | |
| [epub]="Ebooks" | |
| [mobi]="Ebooks" | |
| # Images | |
| [jpg]="Images" | |
| [jpeg]="Images" | |
| [png]="Images" | |
| [gif]="Images" | |
| [svg]="Images" | |
| [webp]="Images" | |
| # Videos | |
| [mp4]="Videos" | |
| [mov]="Videos" | |
| [mkv]="Videos" | |
| # Audio | |
| [mp3]="Audio" | |
| [wav]="Audio" | |
| # Archives | |
| [zip]="Archives" | |
| [tar]="Archives" | |
| [gz]="Archives" | |
| [rar]="Archives" | |
| [7z]="Archives" | |
| # Scripts/Code | |
| [sh]="Scripts" | |
| [py]="Scripts" | |
| [js]="Scripts" | |
| [css]="Scripts" | |
| [html]="Scripts" | |
| # Executables/Packages | |
| [deb]="Packages" | |
| [rpm]="Packages" | |
| [bin]="Packages" | |
| [AppImage]="Packages" | |
| # Firefox Extension | |
| [xpi]="Firefox Extension" | |
| # Android APK | |
| [apk]="Android APK" | |
| [apkg]="Android APK" | |
| # Running Routes | |
| [fit]="Routes" | |
| [gpx]="Routes" | |
| ) | |
| # Loop through all the files in the Downloads directory. | |
| for file in "$DOWNLOADS_DIR"/*; do | |
| # Check if the item is a regular file (not a directory or link). | |
| if [ -f "$file" ]; then | |
| # Get the filename from the full path. | |
| filename=$(basename -- "$file") | |
| # Extract the extension by removing everything up to the last dot. | |
| extension="${filename##*.}" | |
| # Check if the file's extension exists as a key in our associative array. | |
| if [[ -n "${file_types[$extension]}" ]]; then | |
| # Get the destination directory name from the array. | |
| dest_dir_name="${file_types[$extension]}" | |
| DEST_DIR="$DOWNLOADS_DIR/$dest_dir_name" | |
| # Check if the destination directory exists. If not, create it. | |
| if [ ! -d "$DEST_DIR" ]; then | |
| echo "Creating new directory: $DEST_DIR" | |
| mkdir -p "$DEST_DIR" | |
| fi | |
| # Move the file to the destination directory. | |
| echo " -> Moving '$filename' to '$dest_dir_name'..." | |
| mv -v "$file" "$DEST_DIR/" | |
| else | |
| # If the file extension is not in the list, it's ignored. | |
| echo " -> Ignoring '$filename' (unknown file type)." | |
| fi | |
| fi | |
| done | |
| echo "-----------------------------------------------------" | |
| echo "Organization complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment