Skip to content

Instantly share code, notes, and snippets.

@OdinsHat
Created August 8, 2025 16:36
Show Gist options
  • Save OdinsHat/78b8355184797bf9382c0eda029d6344 to your computer and use it in GitHub Desktop.
Save OdinsHat/78b8355184797bf9382c0eda029d6344 to your computer and use it in GitHub Desktop.

Revisions

  1. OdinsHat created this gist Aug 8, 2025.
    123 changes: 123 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    #!/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."