Clean Up Your Downloads Folder: A Bash Automation Script for Mac/Linux

Clean Up Your Downloads Folder: A Bash Automation Script for Mac/Linux

Clean Up Your Downloads Folder: A Bash Automation Script for Mac/Linux

 

Does your ~/Downloads folder look like a digital junk drawer? PDFs next to random installers, screenshots neighbors with ZIP files you forgot about? As developers and power users, we can automate this clutter away with a simple Bash script. In this post, we’ll walk through building a shell-based automation tool that sorts your downloads by file type or modification month—perfect for weekend productivity or daily system hygiene.

1. Why Automate Downloads Folder Cleanup?

Cleaning your Downloads folder isn’t just about aesthetics—it’s about reducing mental load, improving searchability, and keeping your most-used workspace efficient. Instead of manually dragging files into folders, we’ll write a script that:

  • Scans the Downloads directory
  • Identifies file types or modification dates
  • Moves files into categorized subfolders

This works seamlessly on any Unix-like system, including macOS and Linux.

2. Getting Started: Creating the Script Framework

We’ll create a Bash script called sort_downloads.sh. Let’s start with the basic structure:

#!/bin/bash

DOWNLOADS_DIR="$HOME/Downloads"
ORGANIZE_BY="type"  # or set to "month"

mkdir -p "$DOWNLOADS_DIR"  # Ensure the directory exists
cd "$DOWNLOADS_DIR" || exit

We’re setting the base directory and allowing toggling between organizing by file type or modification month. This gives flexibility to the user.

3. Organizing Files by Type

Sorting files by type allows us to group files into formats like PDFs, Images, and Archives. The script uses find and a loop to classify files dynamically. Here’s how it works:

if [ "$ORGANIZE_BY" == "type" ]; then
  for FILE in *; do
    if [ -f "$FILE" ]; then
      EXTENSION="${FILE##*.}"
      FOLDER="${EXTENSION,,}_files"  # lowercase folder
      mkdir -p "$FOLDER"
      mv "$FILE" "$FOLDER/"
    fi
  done
fi

Explanation:

  • ${FILE##*.} extracts the file extension.
  • ${EXTENSION,,} converts extension to lowercase.
  • Files are moved to dynamic directories like pdf_files, jpg_files, etc.

This approach scales well for an ever-growing variety of files.

4. Organizing Files by Month

Sometimes, it makes more sense to group files by when they were downloaded. Organizing by last modification month helps with version tracking or identifying stale files. Here’s the code:

if [ "$ORGANIZE_BY" == "month" ]; then
  for FILE in *; do
    if [ -f "$FILE" ]; then
      MOD_DATE=$(stat -c %y "$FILE" 2>/dev/null || stat -f %Sm -t "%Y-%m" "$FILE")
      MONTH=$(date -d "$MOD_DATE" "+%Y-%m" 2>/dev/null || date -j -f "%Y-%m" "$MOD_DATE" +"%Y-%m")
      mkdir -p "$MONTH"
      mv "$FILE" "$MONTH/"
    fi
  done
fi

Cross-compatible for both Linux and macOS, the script gracefully handles different stat and date commands. Example month folders: 2024-04.

5. Adding Cron for Automation

Why run this script manually when you can automate it with cron? Here’s how to trigger it daily via crontab:

crontab -e

Add this line:

0 8 * * * /path/to/sort_downloads.sh

This runs the script every day at 8am automatically. Be sure to make it executable:

chmod +x sort_downloads.sh

Tip: Always use absolute paths in cron since it runs in a limited shell.

6. Tips, Edge Cases, and Enhancements

Before deploying, consider these improvements:

  • Dry run option: Add a --dry-run flag to preview changes first.
  • Conflict resolution: Handle duplicate filenames with mv -n or mv -i.
  • Log activity: Direct output to a log file for auditing: mv "$FILE" "$FOLDER/" >> cleanup.log

This script demonstrates how a combination of bash, stat, and cron can help you reclaim order in a chaotic Downloads folder—with automation that’s adaptable and maintainable.

Conclusion

With about 30 lines of Bash, we’ve built a handy automation tool that cleans your Downloads folder like a digital Roomba. Customize it based on your workflow, set it up to run regularly, and enjoy a more productive, visually satisfying work environment. Small scripts like this are great stepping stones toward larger system automation projects.

 

Useful links: