批量文件夹清理脚本需求:仅删除全为超1年文件的文件夹
Hey there! Let's tackle your two scripting requirements—since you're dealing with a large number of folders, we'll focus on efficiency and safety to avoid any accidental data loss.
This one's straightforward: we need to locate all files in a target folder (and optionally its subdirectories) that haven't been modified in over a year, then delete them. The find command is perfect here because it's fast and handles large file sets well.
Bash Script Options
Option 1: Delete only files in the target folder (no subdirectories)
# Replace /path/to/your/folder with your actual folder path find /path/to/your/folder -maxdepth 1 -type f -mtime +365 -delete
Option 2: Recursively delete files in subdirectories too
find /path/to/your/folder -type f -mtime +365 -delete
What each parameter does:
-maxdepth 1: Restricts the search to only the top-level folder (remove this if you want to include subdirectories)-type f: Targets only files (ignores folders themselves)-mtime +365: Selects files modified more than 365 days ago (use-atimefor access time or-ctimefor change time if needed)-delete: Deletes the matching files (always test first without this flag!)
Critical Pre-Test Step
Before running the delete command, replace -delete with -print to see exactly which files will be removed:
find /path/to/your/folder -maxdepth 1 -type f -mtime +365 -print
This requires a bit more logic: for each folder, we need to check if every single file inside it (and its subdirectories, if applicable) is older than a year. If even one file is newer, we keep the folder; if all are old, we delete the entire folder.
Bash Script
# Replace /path/to/parent/folder with the directory containing all your target folders for dir in /path/to/parent/folder/*/; do # Check if there's ANY file in the folder modified within the last year # -quit stops searching as soon as one recent file is found (saves time!) has_recent_file=$(find "$dir" -type f -mtime -365 -print -quit) if [ -z "$has_recent_file" ]; then # No recent files found—delete the entire folder rm -rf "$dir" echo "Deleted old folder: $dir" else echo "Kept folder (contains recent files): $dir" fi done
Key Notes:
- The
/*/at the end of the parent folder path ensures we only target subdirectories (not files in the parent itself) -mtime -365looks for files modified less than 365 days ago (the opposite of our first script)-quitmakes the check much faster for large folders—we don't need to scan every file if we find one recent one immediately- Always test first: Replace
rm -rf "$dir"withecho "Would delete: $dir"to verify which folders are marked for deletion
Efficiency Tip for Massive Folder Sets
If you're dealing with thousands of folders, you can optimize this by using find to generate the list of directories and pipe it into a loop, but the above script is more readable for most use cases.
Important Safety Reminders
- Always back up your data before running any deletion scripts
- Run scripts with the minimum necessary permissions (avoid using
sudounless absolutely required) - Adjust the
-mtimevalue if you need to use a different time frame (e.g.,+360for 10 months,-30for the last 30 days)
内容的提问来源于stack exchange,提问作者Pete White

