<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alpha Nsimba Kasanji</title>
    <description>The latest articles on DEV Community by Alpha Nsimba Kasanji (@alpha_nsimbakasanji_c8b2).</description>
    <link>https://dev.to/alpha_nsimbakasanji_c8b2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3164953%2F78b858f9-610f-436e-b456-a838958e2d94.jpg</url>
      <title>DEV Community: Alpha Nsimba Kasanji</title>
      <link>https://dev.to/alpha_nsimbakasanji_c8b2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alpha_nsimbakasanji_c8b2"/>
    <language>en</language>
    <item>
      <title>Automatically Sort and Organize Your Downloads Folder on Linux Using Python By Alpha Nsimba Kasanji </title>
      <dc:creator>Alpha Nsimba Kasanji</dc:creator>
      <pubDate>Thu, 15 May 2025 16:16:36 +0000</pubDate>
      <link>https://dev.to/alpha_nsimbakasanji_c8b2/automatically-sort-and-organize-your-downloads-folder-on-linux-using-pythonby-alpha-nsimba-kasanji-1lb7</link>
      <guid>https://dev.to/alpha_nsimbakasanji_c8b2/automatically-sort-and-organize-your-downloads-folder-on-linux-using-pythonby-alpha-nsimba-kasanji-1lb7</guid>
      <description>&lt;p&gt;If your Downloads folder is always overflowing with random files, and you're constantly digging through it to find what you need - it's time to automate the chaos.&lt;br&gt;
In this tutorial, you'll learn how to create a Python script that automatically organizes your Downloads folder by sorting files into subfolders based on their file types (images, documents, videos, etc.). We'll also show how to run this script automatically at regular intervals using cron.&lt;/p&gt;

&lt;p&gt;What You'll Need&lt;br&gt;
A Linux machine (we'll be using Debian-based systems)&lt;br&gt;
Python 3 installed (which comes pre-installed on most distributions)&lt;br&gt;
Basic knowledge of terminal commands&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create Your Python Script&lt;br&gt;
First, open your terminal and create the Python script:&lt;br&gt;
touch sort_downloads.py&lt;br&gt;
chmod +x sort_downloads.py&lt;br&gt;
This command creates an empty file and makes it executable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The Script: Sort Files by Extension&lt;br&gt;
Open the file with a text editor:&lt;br&gt;
nano ~/sort_downloads.py&lt;br&gt;
Paste the following Python code:&lt;/p&gt;
&lt;h1&gt;
  
  
  !/usr/bin/env python3
&lt;/h1&gt;

&lt;p&gt;import os&lt;br&gt;
import shutil&lt;br&gt;
from pathlib import Path&lt;/p&gt;
&lt;h1&gt;
  
  
  Define the Downloads directory
&lt;/h1&gt;

&lt;p&gt;downloads_dir = Path.home() / "Downloads"&lt;/p&gt;
&lt;h1&gt;
  
  
  Define destination directories and file extensions
&lt;/h1&gt;

&lt;p&gt;categories = {&lt;br&gt;
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],&lt;br&gt;
"Documents": [".pdf", ".docx", ".txt", ".pptx", ".xlsx"],&lt;br&gt;
"Videos": [".mp4", ".avi", ".mkv", ".mov", ".flv"],&lt;br&gt;
"Archives": [".zip", ".tar.gz", ".rar"],&lt;br&gt;
"Audio": [".mp3", ".wav", ".ogg"],&lt;br&gt;
"Misc": []  # For unrecognized files&lt;br&gt;
}&lt;/p&gt;
&lt;h1&gt;
  
  
  Create subdirectories if they don't exist
&lt;/h1&gt;

&lt;p&gt;for category in categories:&lt;br&gt;
folder = downloads_dir / category&lt;br&gt;
folder.mkdir(exist_ok=True)&lt;/p&gt;
&lt;h1&gt;
  
  
  Move files into appropriate folders
&lt;/h1&gt;

&lt;p&gt;for item in downloads_dir.iterdir():&lt;br&gt;
if item.is_file():&lt;br&gt;
    moved = False&lt;br&gt;
    for category, extensions in categories.items():&lt;br&gt;
        for ext in extensions:&lt;br&gt;
            if item.name.lower().endswith(ext):&lt;br&gt;
                shutil.move(str(item), str(downloads_dir / category / item.name))&lt;br&gt;
                moved = True&lt;br&gt;
                break&lt;br&gt;
        if moved:&lt;br&gt;
            break&lt;br&gt;
    if not moved:&lt;br&gt;
        shutil.move(str(item), str(downloads_dir / "Misc" / item.name))&lt;br&gt;
print("Downloads folder organized successfully!")&lt;br&gt;
Save and exit (CTRL + O, then CTRL + X in nano).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the Script&lt;br&gt;
To test your script, run:&lt;br&gt;
python3 ~/sort_downloads.py&lt;br&gt;
Your Downloads folder should now be neatly organized into categories like Images, Documents, Videos, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Automate the Script with Cron&lt;br&gt;
To keep your folder clean continuously, you can schedule the script to run automatically.&lt;br&gt;
Open the crontab editor:&lt;br&gt;
crontab -e&lt;br&gt;
Add the following line to run the script every hour:&lt;br&gt;
0 * * * * /usr/bin/python3 /home/yourusername/sort_downloads.py&lt;br&gt;
Make sure to replace /home/yourusername/ with your actual username. You can find the path to Python with:&lt;br&gt;
which python3&lt;br&gt;
Save and exit the editor.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Why This Works&lt;br&gt;
Cross-platform: This script can run on any Linux distro with Python installed.&lt;br&gt;
Customizable: Add more categories and extensions as needed.&lt;br&gt;
Low maintenance: Once set up, it just works quietly in the background.&lt;br&gt;
No clutter: Keeps your Downloads folder clean and manageable.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
With just a few lines of Python and a simple cron job, you've turned your chaotic Downloads folder into a well-organized space. This little script saves time, reduces clutter, and makes your workflow more efficient - all without lifting a finger after setup.&lt;br&gt;
Happy automating!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
