如何将网站uploads文件夹存量媒体文件回溯整理至年月文件夹?
Hey there, I’ve dealt with exactly this kind of messy uploads folder before—8GB of images all dumped in one place is a nightmare for management, let alone backups or troubleshooting. Let’s break down some efficient, low-risk ways to get them organized into year/month subfolders:
Before you touch a single file, make a full, verified backup of your entire uploads folder. 8GB is a significant amount of data, and you don’t want to lose anything if a script glitches or you make a mistake. Use reliable tools for this:
- Linux/macOS: Use
rsyncfor a fast, verifiable copy:rsync -av /path/to/your/uploads /path/to/uploads_backup - Windows: Use Robocopy via Command Prompt to preserve file metadata:
robocopy C:\path\to\uploads C:\path\to\uploads_backup /E /COPYALL
Most content management systems have built-in or plugin-based solutions that handle both file movement and database updates (so your site doesn’t break links):
- Use a dedicated plugin: Look for plugins designed to organize media files. These tools can automatically scan your existing uploads, sort files into year/month folders based on their upload date, and update all the internal database references to match the new paths.
- Hands-on script approach: If you prefer not to use plugins, you can write a script that:
- Reads each file’s creation/upload date
- Creates the corresponding year/month subfolders
- Moves the files
- Updates your CMS database (for WordPress, this means editing the
wp_postsandwp_postmetatables to reflect the new file paths)
If you’re managing files directly on the server without a CMS, script-based sorting is the fastest way to go:
Linux/macOS Bash Script
This script loops through your image files, sorts them into year/month folders based on their modification date, and moves them safely:
#!/bin/bash cd /path/to/your/uploads # Define image extensions to target image_extensions=("jpg" "jpeg" "png" "gif" "webp" "svg") for ext in "${image_extensions[@]}"; do for file in *.$ext; do if [ -f "$file" ]; then # Extract year and month from file modification date year=$(date -r "$file" +%Y) month=$(date -r "$file" +%m) # Create folder if it doesn't exist mkdir -p "$year/$month" # Move the file (add -n to avoid overwriting duplicates) mv -n "$file" "$year/$month/" fi done done
Save this as sort_images.sh, make it executable with chmod +x sort_images.sh, and run it.
Windows PowerShell Script
For Windows servers or local machines, this PowerShell script does the same job:
$uploadsPath = "C:\path\to\your\uploads" $imageExtensions = @("*.jpg", "*.jpeg", "*.png", "*.gif", "*.webp", "*.svg") Get-ChildItem -Path $uploadsPath -File -Include $imageExtensions | ForEach-Object { $year = $_.LastWriteTime.Year $month = $_.LastWriteTime.ToString("MM") $targetFolder = Join-Path -Path $uploadsPath -ChildPath "$year\$month" # Create folder if missing if (-not (Test-Path -Path $targetFolder)) { New-Item -ItemType Directory -Path $targetFolder | Out-Null } # Move file, avoid overwriting duplicates Move-Item -Path $_.FullName -Destination $targetFolder -ErrorAction SilentlyContinue }
Open PowerShell, navigate to the script’s location, and run it with .\Sort-Images.ps1.
GUI Tools (No Coding Needed)
If scripts aren’t your thing, use these visual tools:
- Windows: Bulk Rename Utility lets you set up rules to move files into date-based folders via its "Move to Subfolder" feature.
- macOS: Use Automator to create a workflow that sorts files by modification date into year/month subfolders.
- Test links: If your site references these files directly (not via a CMS), make sure to update all URLs in your code or database to point to the new
/uploads/YYYY/MM/filename.jpgpaths. - Check duplicates: If you used the
-n(Bash) or-ErrorAction SilentlyContinue(PowerShell) flags, go back and handle any duplicate files that weren’t moved. - Verify backup: Double-check that your backup is intact before deleting any original files (though the scripts above just move files, not delete, it’s still a good habit).
内容的提问来源于stack exchange,提问作者Mauricio

