DEV Community

Cover image for Automate Your Linux Downloads
Sakib MD Nazmush
Sakib MD Nazmush

Posted on

Automate Your Linux Downloads

If you work online a lot like I do, chances are your Downloads folder turns into a total mess by the end of the week. Mine’s usually packed with project docs, design files, code snippets, client stuff, images from random platforms, you name it. By Friday, it’s a digital junkyard.

Every weekend, I’d sit down and spend way too much time cleaning it up. Making folders, sorting files, deleting the junk. Over and over again.

So I finally decided to fix it. With the help of Claude AI, I built a simple but powerful Linux script that does all the organizing for me. Automatically and in real time. It sorts everything by where it came from (WhatsApp, Facebook, LinkedIn, etc.) and what type of file it is (images, docs, videos—you get the idea).

No more weekend cleanup sessions. Just clean folders and peace of mind.

Problem: Download Folder Chaos

Whether you're working on a big project or just learning something new, downloading files is part of daily life. From WhatsApp screenshots and Facebook images to LinkedIn documents. Your Downloads folder ends up catching it all.

Maybe you're juggling:

  • Social media assets like images, memes, or screenshots

  • Project resources such as PDFs, spreadsheets, design files, or bits of code

  • Client materials sent over email or chat in all kinds of formats

  • Research stuff—articles, reference images, even tutorial videos

Solution: Auto-Organization

I created a smart bash script that monitors your Downloads folder in real-time and automatically sorts files based on:

  • Source identification (WhatsApp, Facebook, Instagram, LinkedIn, etc.)
  • File type categorization (Images, Documents, Videos, Audio, etc.)
  • Smart pattern matching for platform-specific naming conventions

Prerequisites

Before we dive into the implementation, ensure you have:

  • A Linux system (Ubuntu, Debian, or similar)
  • Basic terminal knowledge
  • inotify-tools package for file monitoring

Step 1: Install Required Dependencies

First, install the file monitoring tools:

sudo apt update
sudo apt install inotify-tools
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Folder Structure

Set up organized directories in your Downloads folder:

mkdir -p ~/Downloads/{WhatsApp,Facebook,Instagram,Twitter,LinkedIn,YouTube,Reddit,Images,Documents,Videos,Audio,Archives,Software,Torrents,PDFs,Spreadsheets,Presentations,Code,Fonts,General}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Smart Organizer Script

Navigate to your home directory and create the script:

cd ~ nano download-organizer.sh
Enter fullscreen mode Exit fullscreen mode

Now, paste the following comprehensive script:

#!/bin/bash

# Create comprehensive folder structure
mkdir -p ~/Downloads/{WhatsApp,Facebook,Instagram,Twitter,LinkedIn,YouTube,Reddit,Images,Documents,Videos,Audio,Archives,Software,Torrents,PDFs,Spreadsheets,Presentations,Code,Fonts,General}

echo "Starting comprehensive download organizer..."
echo "Monitoring ~/Downloads for ALL new files..."

# Monitor Downloads folder for new files
inotifywait -m ~/Downloads -e create -e moved_to --format '%w%f' |
while read file; do
    # Skip if it's a directory or temporary/incomplete file
    if [[ -d "$file" ]] || [[ "$file" == *".tmp" ]] || [[ "$file" == *".crdownload" ]] || [[ "$file" == *".part" ]] || [[ "$file" == *".download" ]]; then
        continue
    fi

    # Wait for file to be completely written
    sleep 3

    # Check if file still exists
    if [[ ! -f "$file" ]]; then
        continue
    fi

    filename=$(basename "$file")
    extension="${filename##*.}"
    extension_lower=$(echo "$extension" | tr '[:upper:]' '[:lower:]')

    echo "New file detected: $filename"

    # First check for source-based patterns (social media, websites)
    case "$filename" in
        *WhatsApp*|*whatsapp*|*WA[0-9]*|*IMG-*-WA[0-9]*) 
            mv "$file" ~/Downloads/WhatsApp/
            echo "→ Moved to WhatsApp folder: $filename"
            continue
            ;;
        *facebook*|*Facebook*|*FB_IMG*|*fb_*) 
            mv "$file" ~/Downloads/Facebook/
            echo "→ Moved to Facebook folder: $filename"
            continue
            ;;
        *instagram*|*Instagram*|*IG_*) 
            mv "$file" ~/Downloads/Instagram/
            echo "→ Moved to Instagram folder: $filename"
            continue
            ;;
        *twitter*|*Twitter*|*tweet*) 
            mv "$file" ~/Downloads/Twitter/
            echo "→ Moved to Twitter folder: $filename"
            continue
            ;;
        *linkedin*|*LinkedIn*) 
            mv "$file" ~/Downloads/LinkedIn/
            echo "→ Moved to LinkedIn folder: $filename"
            continue
            ;;
        *youtube*|*YouTube*|*yt_*) 
            mv "$file" ~/Downloads/YouTube/
            echo "→ Moved to YouTube folder: $filename"
            continue
            ;;
        *reddit*|*Reddit*) 
            mv "$file" ~/Downloads/Reddit/
            echo "→ Moved to Reddit folder: $filename"
            continue
            ;;
    esac

    # Then organize by file type
    case "$extension_lower" in
        # Images
        jpg|jpeg|png|gif|bmp|webp|svg|tiff|tif|ico|heic|heif|raw|cr2|nef|orf|sr2|arw) 
            mv "$file" ~/Downloads/Images/
            echo "→ Moved to Images folder: $filename"
            ;;

        # PDFs
        pdf) 
            mv "$file" ~/Downloads/PDFs/
            echo "→ Moved to PDFs folder: $filename"
            ;;

        # Documents
        doc|docx|odt|rtf|txt|md|tex) 
            mv "$file" ~/Downloads/Documents/
            echo "→ Moved to Documents folder: $filename"
            ;;

        # Spreadsheets
        xls|xlsx|ods|csv) 
            mv "$file" ~/Downloads/Spreadsheets/
            echo "→ Moved to Spreadsheets folder: $filename"
            ;;

        # Presentations
        ppt|pptx|odp) 
            mv "$file" ~/Downloads/Presentations/
            echo "→ Moved to Presentations folder: $filename"
            ;;

        # Videos
        mp4|avi|mkv|mov|wmv|flv|webm|m4v|3gp|ogv|ts|m2ts|vob|f4v) 
            mv "$file" ~/Downloads/Videos/
            echo "→ Moved to Videos folder: $filename"
            ;;

        # Audio
        mp3|wav|flac|aac|ogg|wma|m4a|opus|aiff|au) 
            mv "$file" ~/Downloads/Audio/
            echo "→ Moved to Audio folder: $filename"
            ;;

        # Archives
        zip|rar|7z|tar|gz|bz2|xz|z|tgz|tbz2|txz|lzma|cab|iso|dmg) 
            mv "$file" ~/Downloads/Archives/
            echo "→ Moved to Archives folder: $filename"
            ;;

        # Software/Executables
        exe|msi|deb|rpm|pkg|dmg|app|appimage|snap|flatpak|run) 
            mv "$file" ~/Downloads/Software/
            echo "→ Moved to Software folder: $filename"
            ;;

        # Torrents
        torrent) 
            mv "$file" ~/Downloads/Torrents/
            echo "→ Moved to Torrents folder: $filename"
            ;;

        # Code files
        py|js|html|css|php|java|cpp|c|h|sh|bat|ps1|sql|json|xml|yaml|yml) 
            mv "$file" ~/Downloads/Code/
            echo "→ Moved to Code folder: $filename"
            ;;

        # Fonts
        ttf|otf|woff|woff2|eot) 
            mv "$file" ~/Downloads/Fonts/
            echo "→ Moved to Fonts folder: $filename"
            ;;

        # Everything else
        *) 
            mv "$file" ~/Downloads/General/
            echo "→ Moved to General folder: $filename"
            ;;
    esac
done
Enter fullscreen mode Exit fullscreen mode

Step 4: Make the Script Executable

chmod +x download-organizer.sh
Enter fullscreen mode Exit fullscreen mode

Step 5: Test the Script

Before setting up automation, test the script manually:

./download-organizer.sh
Enter fullscreen mode Exit fullscreen mode

You should see:

Starting comprehensive download organizer...
Monitoring ~/Downloads for ALL new files...
Enter fullscreen mode Exit fullscreen mode

Try downloading a file to test the organization!

Step 6: Set Up Automatic Startup

To make this script run automatically when your system boots, we'll create a systemd service:

Create the Service File

mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/download-organizer.service
Enter fullscreen mode Exit fullscreen mode

Add this configuration:

[Unit]
Description=Download Organizer - Auto File Organization
After=graphical-session.target

[Service]
Type=simple
ExecStart=/home/YOUR_USERNAME/download-organizer.sh
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=default.target
Enter fullscreen mode Exit fullscreen mode

Important: Replace YOUR_USERNAME with your actual username.

Enable and Start the service.

systemctl --user daemon-reload
systemctl --user enable download-organizer.service
systemctl --user start download-organizer.service
Enter fullscreen mode Exit fullscreen mode

Results and Benefits

Since implementing this system, I've experienced:

  • 90% Time Savings: No more weekend cleanup sessions
  • Instant Organization: Files are sorted the moment they're downloaded
  • Easy Cleanup: Platform-specific folders make bulk deletion effortless
  • Improved Productivity: Finding files is now instant and intuitive

Weekend Cleanup Made Easy

Now, instead of spending hours sorting files, my weekend cleanup is simple:

# Delete all WhatsApp files from this week
rm -rf ~/Downloads/WhatsApp/*

# Clear out temporary project files
rm -rf ~/Downloads/General/*

# Keep important documents and clean the rest
Enter fullscreen mode Exit fullscreen mode

Try implementing this system, and say goodbye to download folder chaos forever. Your future self will thank you every weekend when you can focus on what matters most instead of sorting files.

Found this helpful? Give it a 🤍 and share it with fellow developers who are tired of download folder mayhem.

Top comments (0)