本文目录导读:

我来为您提供几个不同场景下的回收站清理脚本。
Windows 回收站清理脚本
使用 PowerShell(推荐)
# clear_recycle_bin.ps1 # 清理当前用户的回收站 Clear-RecycleBin -Force -ErrorAction SilentlyContinue # 清理所有用户的回收站(需要管理员权限) Clear-RecycleBin -DriveLetter C -Force -ErrorAction SilentlyContinue Clear-RecycleBin -DriveLetter D -Force -ErrorAction SilentlyContinue # 输出清理完成信息 Write-Host "回收站清理完成!" -ForegroundColor Green
使用批处理脚本(.bat)
@echo off清理回收站工具
color 0A
echo ========================================
echo 清理回收站工具 v1.0
echo ========================================
echo.
:: 检查管理员权限
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [错误] 请以管理员身份运行此脚本!
pause
exit /b 1
)
:: 清理所有驱动器的回收站
echo 正在清理回收站...
rd /s /q C:\$Recycle.bin 2>nul
rd /s /q D:\$Recycle.bin 2>nul
rd /s /q E:\$Recycle.bin 2>nul
:: 创建新的回收站目录(如果不存在)
mkdir C:\$Recycle.bin 2>nul
mkdir D:\$Recycle.bin 2>nul
mkdir E:\$Recycle.bin 2>nul
echo.
echo [完成] 回收站已清理!
echo.
:: 显示清理前回收站大小(可选)
powershell -Command "Get-ChildItem -Path 'C:\$Recycle.bin' -Recurse -Force | Measure-Object -Property Length -Sum | Select-Object Sum"
pause
定时清理脚本(计划任务版)
# scheduled_recycle_cleaner.ps1
# 需要配合 Windows 任务计划程序使用
param(
[int]$DaysToKeep = 7, # 保留7天内的文件
[string]$LogPath = "C:\Logs\recycle_cleanup.log"
)
# 创建日志目录
$logDir = Split-Path $LogPath -Parent
if (!(Test-Path $logDir)) {
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
}
# 记录日志函数
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Out-File -Append -FilePath $LogPath
Write-Host "$timestamp - $Message"
}
# 开始清理
Write-Log "开始清理回收站..."
try {
# 清空回收站
Clear-RecycleBin -Force -ErrorAction Stop
Write-Log "回收站清理成功"
} catch {
Write-Log "回收站清理失败: $($_.Exception.Message)"
}
# 清理临时文件(可选)
$tempFolders = @(
"$env:TEMP",
"C:\Windows\Temp"
)
foreach ($folder in $tempFolders) {
if (Test-Path $folder) {
try {
# 删除超过指定天数的文件
Get-ChildItem $folder -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$DaysToKeep) } |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Write-Log "清理完成: $folder"
} catch {
Write-Log "清理失败: $folder - $($_.Exception.Message)"
}
}
}
Write-Log "清理任务完成"
Linux 系统回收站清理
定时清理脚本
#!/bin/bash
# cleanup_trash.sh
# 清理普通用户回收站
clean_user_trash() {
local user_home="$1"
local trash_path="$user_home/.local/share/Trash"
if [ -d "$trash_path" ]; then
echo "清理 $user_home 的回收站..."
find "$trash_path" -mindepth 1 -delete 2>/dev/null
echo "完成"
fi
}
# 清理过期文件(保留30天)
clean_old_files() {
local trash_path="$1"
local days="${2:-30}"
if [ -d "$trash_path" ]; then
echo "清理30天前的文件..."
find "$trash_path" -type f -mtime +$days -delete 2>/dev/null
echo "完成"
fi
}
# 主函数
main() {
echo "=========================================="
echo " Linux 回收站清理工具"
echo "=========================================="
# 清理当前用户的回收站
clean_user_trash "$HOME"
# 清理其他用户的回收站(需要root权限)
if [ "$EUID" -eq 0 ]; then
for user_home in /home/*; do
clean_user_trash "$user_home"
done
fi
# 清理系统临时文件
echo "清理系统临时文件..."
# 清理 /tmp 下超过7天的文件
find /tmp -type f -mtime +7 -delete 2>/dev/null
# 清理 /var/tmp 下超过30天的文件
find /var/tmp -type f -mtime +30 -delete 2>/dev/null
echo "=========================================="
echo "清理完成!"
echo "=========================================="
}
# 执行主函数
main
配合 cron 定时任务
# 创建 crontab 条目(每星期天凌晨2点清理) 0 2 * * 0 /path/to/cleanup_trash.sh >> /var/log/trash_cleanup.log 2>&1 # 或每天清理 # 30 3 * * * /path/to/cleanup_trash.sh
设置定时任务
Windows 计划任务设置
# setup_scheduled_job.ps1 # 注册计划任务,每天凌晨2点执行 $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File C:\Scripts\clear_recycle_bin.ps1" $trigger = New-ScheduledTaskTrigger -Daily -At "02:00AM" $settings = New-ScheduledTaskSettingsSet -Force -StartWhenAvailable $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest Register-ScheduledTask -TaskName "RecycleBinCleaner" -Action $action -Trigger $trigger -Settings $settings -Principal $principal Write-Host "定时任务已注册!每天凌晨2点自动清理回收站"
使用方法
- 手动清理:直接运行脚本
- 定时清理:使用计划任务或cron
- Logging:日志记录便于追踪
注意事项
- ⚠️ 通常需要管理员/root权限
- 🔒 重要数据请提前备份
- 📁 建议先测试环境试用
- 📊 可以记录清理前后的大小
您可以根据需求选择合适的脚本,或者告诉我您的具体使用场景,我可以帮您定制更合适的清理方案。