Digital clutter, especially in folders like Downloads or Documents, can quickly become overwhelming. Manually sorting files is a tedious chore thatβs easy to put off. Fortunately, with a little bit of bash scripting, you can automate this process entirely.
This guide will walk you through creating a simple but powerful bash script that organizes files into subfolders named after each fileβs modification date (e.g., 2025-11-01).
The Goal
Our objective is to take a directory full of miscellaneous files and sort them.
Before:
/Downloads
βββ report.pdf
βββ screenshot-1.png
βββ project.zip
βββ image_final.jpg
βββ notes.txt
βββ another-screenshot.png
After:
/Downloads
βββ 2025-10-30/
β βββ report.pdf
βββ 2025-10-31/
β βββ project.zip
β βββ image_final.jpg
βββ 2025-11-01/
βββ screenshot-1.png
βββ notes.txt
βββ another-screenshot.png
Letβs build the script to do this.
Step 1: Understanding the Core Commands
Our script will rely on a few fundamental Linux commands working together.
forloop: To iterate over every item in a directory. The basic syntax isfor item in *.date: To get the last modification date of a file. Weβll usedate -r filename +%F.-r filename: Gets the date from the specified file instead of the current system time.+%F: Formats the date asYYYY-MM-DD. This is a convenient shortcut for%Y-%m-%d.
mkdir: To create our dated folders. Weβll usemkdir -p dirname.-p: The βparentβ flag. It creates the directory without throwing an error if it already exists, which is perfect for our script.
mv: To move the files into their new folders. The syntax ismv source destination.
Step 2: Writing the Bash Script
Now, letβs combine these commands into a script. Create a new file named organize_files.sh and add the following content.
#!/bin/bash
# A script to organize files in the current directory into dated subfolders.
# Loop through all items in the current directory.
# The quotes around "*" handle filenames with spaces.
for file in *
do
# Check if the item is a file (and not a directory or this script itself).
if [ -f "$file" ]; then
# Get the file's last modification date in YYYY-MM-DD format.
mod_date=$(date -r "$file" +%F)
# Create a directory for that date if it doesn't already exist.
# The -p flag prevents errors if the directory is already there.
mkdir -p "$mod_date"
# Move the file into its new dated directory.
# The -v flag (verbose) is optional but helpful for seeing what's happening.
mv -v "$file" "$mod_date/"
fi
done
echo "File organization complete."
Script Breakdown:
#!/bin/bash: This βshebangβ tells the system to execute the script using the bash shell.for file in *: This starts a loop that assigns the name of each item in the directory to thefilevariable, one by one.if [ -f "$file" ]: This is a crucial safety check. It tests if the item is a regular file (-f). This prevents the script from trying to move subdirectories or itself.mod_date=$(date ...): This runs thedatecommand and stores its output (YYYY-MM-DD) in a variable namedmod_date.mkdir -p "$mod_date": This creates the destination folder (e.g.,2025-11-01).mv -v "$file" "$mod_date/": This moves the current file into its corresponding dated folder. We use quotes around variables like"$file"to ensure the script works correctly with filenames containing spaces or special characters.
Step 3: Making the Script Executable and Running It
Before you run the script, save it in the directory you want to organize. For a first run, itβs always wise to test it on a small directory with copied files, not your originals.
- Make the script executable: Open your terminal in the target directory and run the
chmodcommand.chmod +x organize_files.sh - Run the script:
./organize_files.sh
You will see output as the mv command reports each file it moves, thanks to the -v flag.
renamed 'screenshot-1.png' -> '2025-11-01/screenshot-1.png'
renamed 'notes.txt' -> '2025-11-01/notes.txt'
renamed 'report.pdf' -> '2025-10-30/report.pdf'
File organization complete.
Customizing the Script
You can easily adapt this script to fit different needs.
-
Organize by Month: To sort files into
YYYY-MMfolders (e.g.,2025-11), simply change thedateformat.# Change this line in the script mod_date=$(date -r "$file" +%Y-%m) -
Organize Specific File Types: To only sort images, change the
forloop.# Change this line in the script to target only .jpg, .jpeg, and .png files for file in *.jpg *.jpeg *.png
Conclusion
You now have a flexible and powerful bash script for decluttering any directory. By combining a simple for loop with the date, mkdir, and mv commands, you can automate a common file management task.
As a next step, you could adapt this script to take a directory path as an argument or even set it up as a cron job to run automatically on your Downloads folder every day. Happy scripting