5 min read

How to Automatically Organize Files into Dated Folders with a Bash Script

Table of Contents

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.

  1. for loop: To iterate over every item in a directory. The basic syntax is for item in *.
  2. date: To get the last modification date of a file. We’ll use date -r filename +%F.
    • -r filename: Gets the date from the specified file instead of the current system time.
    • +%F: Formats the date as YYYY-MM-DD. This is a convenient shortcut for %Y-%m-%d.
  3. mkdir: To create our dated folders. We’ll use mkdir -p dirname.
    • -p: The β€œparent” flag. It creates the directory without throwing an error if it already exists, which is perfect for our script.
  4. mv: To move the files into their new folders. The syntax is mv 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 the file variable, 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 the date command and stores its output (YYYY-MM-DD) in a variable named mod_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.

  1. Make the script executable: Open your terminal in the target directory and run the chmod command.
    chmod +x organize_files.sh
  2. 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-MM folders (e.g., 2025-11), simply change the date format.

    # Change this line in the script
    mod_date=$(date -r "$file" +%Y-%m)
  • Organize Specific File Types: To only sort images, change the for loop.

    # 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