Ubuntu Recycle 自动清理设置方法
Ubuntu 系统默认的回收站(Recycle Bin)功能未提供直接的自动清理选项,但可通过以下方法实现自动清理,覆盖不同场景需求:
trash-cli 结合 Cron 定时任务trash-cli 是命令行工具,用于管理回收站文件,结合 cron 可实现定期自动清理。
trash-cli:sudo apt update && sudo apt install trash-cli
empty_trash.sh):#!/bin/bash
trash-empty # 清空回收站
chmod +x ~/empty_trash.sh
crontab -e,添加以下行(例如每天凌晨2点清空):0 2 * * * /home/your_username/empty_trash.sh
注:将
/home/your_username/替换为脚本实际路径。
autotrash 工具(推荐)autotrash 是专门用于自动清理回收站的命令行工具,支持按文件驻留时间或剩余磁盘空间触发清理。
autotrash:sudo apt install autotrash
autotrash -d 30
autotrash --max-free 100M -d 30
crontab -e,添加以下行(例如每天凌晨1点检查):0 1 * * * autotrash -d 30
注:
autotrash会自动识别当前用户的回收站路径,无需手动指定。
Ubuntu 桌面环境的隐私设置中,部分版本提供自动清空回收站的选项(如 GNOME 桌面)。
注:此方法依赖桌面环境版本,部分 Ubuntu 衍生版(如 Kubuntu)可能不支持。
若需根据回收站中的文件数量触发清理(如超过100个文件则清空),可创建以下脚本:
clean_recycle_bin.sh):#!/bin/bash
MAX_FILES=100 # 最大文件数阈值
FILE_COUNT=$(ls -1 ~/.local/share/Trash/files/* | wc -l) # 统计回收站文件数
if [ "$FILE_COUNT" -gt "$MAX_FILES" ]; then
trash-empty # 清空回收站
echo "$(date): 回收站文件数超过${MAX_FILES},已清空。" >> ~/recycle_clean.log
fi
chmod +x ~/clean_recycle_bin.sh
crontab -e,添加以下行(例如每小时检查):0 * * * * /home/your_username/clean_recycle_bin.sh
注:脚本会将清理记录保存到
~/recycle_clean.log中,便于排查问题。
/home/username/script.sh),避免 cron 执行失败;cron 中。