Automated Desktop Organizer

[Guide] Automated, Lightweight & Anti-Duplicate Desktop Organizer at Shutdown (Zorin OS Lite)

AI-assisted tutorial, please verify critical steps

This guide explains how to set up a 100% automated and lightweight script that automatically cleans and organizes your Desktop and Downloads folders every time you shut down or reboot your computer.

To ensure total integration with the Universal Backup & Restore Script, this helper script is stored directly inside the .config directory. This means it will be automatically backed up every week into your system architecture without any manual intervention.


🎯 Features

  • Smart File Sorting: Instantly categorizes files by type into dedicated subfolders (Images, Documents, Videos, Music, Archives).
  • Anti-Duplicate Protection: Uses the native Linux --backup=numbered parameter. If a file with the exact same name already exists in the target folder, the script automatically renames the incoming file (e.g., invoice.pdf.~1~) instead of silently overwriting it. Your data is 100% safe.
  • Zero Boot/Runtime Impact: The script runs as a systemd oneshot service during the shutdown sequence. It takes only a few milliseconds to process and will never slow down your computer's startup or daily usage.

Step 1: Create the Sorting Script inside the Protected Config Directory

Open your Terminal (Ctrl + Alt + T), copy-paste the following command block, and press Enter. This creates the script inside your configuration directory:

mkdir -p ~/.config
cat << 'EOF' > ~/.config/organisateur_auto.sh
#!/bin/bash

# Target the Downloads and Desktop folders of the active user
DOSSIERS=("${HOME}/Downloads" "${HOME}/Desktop")

for TARGET in "${DOSSIERS[@]}"; do
    if [ -d "$TARGET" ]; then
        cd "$TARGET" || continue

        # Create subfolders if they do not exist
        mkdir -p Images Documents Videos Music Archives

        # -- ANTI-DUPLICATE SORTING --
        # Moves files based on extensions and auto-renames duplicates instead of erasing them
        find . -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.svg" \) -exec mv --backup=numbered {} Images/ \; 2>/dev/null
        find . -maxdepth 1 -type f \( -iname "*.pdf" -o -iname "*.doc" -o -iname "*.docx" -o -iname "*.odt" -o -iname "*.txt" -o -iname "*.xls" -o -iname "*.xlsx" \) -exec mv --backup=numbered {} Documents/ \; 2>/dev/null
        find . -maxdepth 1 -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.avi" -o -iname "*.mov" \) -exec mv --backup=numbered {} Videos/ \; 2>/dev/null
        find . -maxdepth 1 -type f \( -iname "*.mp3" -o -iname "*.wav" -o -iname "*.flac" -o -iname "*.ogg" \) -exec mv --backup=numbered {} Music/ \; 2>/dev/null
        find . -maxdepth 1 -type f \( -iname "*.zip" -o -iname "*.tar.gz" -o -iname "*.rar" -o -iname "*.7z" \) -exec mv --backup=numbered {} Archives/ \; 2>/dev/null
    fi
done
EOF

Step 2: Make the Script Executable

Grant the system permission to execute the script by running this command in the terminal:

chmod +x ~/.config/organisateur_auto.sh

Step 3: Create the Systemd Shutdown Service

To safely inject the instruction into the system directory without facing permission errors, we use the sudo tee command block. Copy-paste this block and press Enter (type your user password when prompted):

sudo tee /etc/systemd/system/organisateur-extinction.service > /dev/null << 'EOF'
[Unit]
Description=Automated File Sorting for Desktop & Downloads at Shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'for user_home in /home/*; do if [ -d "$user_home" ] && [ -f "$user_home/.config/organisateur_auto.sh" ]; then /bin/bash "$user_home/.config/organisateur_auto.sh"; fi; done'
TimeoutStartSec=10

[Install]
WantedBy=shutdown.target reboot.target halt.target
EOF

Step 4: Enable the System Service permanently

Finally, reload the system manager configuration and permanently enable the automation service so it triggers during every single shutdown or reboot:

sudo systemctl daemon-reload
sudo systemctl enable organisateur-extinction.service

🧪 Test the Setup (Optional)

If you want to trigger the organization process manually right now without waiting to shut down your PC, simply run this command:

~/.config/organisateur_auto.sh

Check your Desktop and Downloads folders: all loose files have been cleanly grouped into their respective subfolders, and any identical names have been safely preserved with a numerical suffix!