You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量文件夹清理脚本需求:仅删除全为超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.


需求1:删除文件夹内所有超过1年的文件

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 -atime for access time or -ctime for 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

需求2:仅删除所有文件均超过1年的文件夹

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 -365 looks for files modified less than 365 days ago (the opposite of our first script)
  • -quit makes 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" with echo "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 sudo unless absolutely required)
  • Adjust the -mtime value if you need to use a different time frame (e.g., +360 for 10 months, -30 for the last 30 days)

内容的提问来源于stack exchange,提问作者Pete White

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:41:28