Linux 回收站优化实用方案
一 容量与自动清理策略
trash-empty --age 7d;安装:sudo apt-get install trash-cli(Debian/Ubuntu)、sudo dnf install trash-cli(Fedora)、sudo yum install trash-cli(RHEL/CentOS)。# /etc/systemd/system/trash-cleanup.service
[Unit]
Description=Custom Trash Cleanup
[Service]
Type=oneshot
ExecStart=/usr/bin/trash-empty --age 7d
示例定时器:# /etc/systemd/system/trash-cleanup.timer
[Unit]
Description=Run trash cleanup daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
启用:sudo systemctl enable --now trash-cleanup.timer。#!/usr/bin/env bash
TRASH="$HOME/.local/share/Trash/files"
MAX_MB=100
CUR_MB=$(du -sm "$TRASH" 2>/dev/null | cut -f1)
if [ "$CUR_MB" -gt "$MAX_MB" ]; then
/usr/bin/trash-empty
echo "Trash cleaned: exceeded ${MAX_MB}MB (was ${CUR_MB}MB)"
fi
加入 crontab(如每 6 小时):0 */6 * * * /path/monitor_trash.sh。max_size = 100M(以实际工具支持为准)。二 命令行与脚本优化
rm 别名到回收站,避免误删。示例:alias rm='trash'(如需保留原始 rm,可用 alias rm='echo "Use trash or /bin/rm"; false' 提示改用回收站)。#!/usr/bin/env bash
TRASH="$HOME/.trash"
mkdir -p "$TRASH"
case "$1" in
delete)
shift
for f; do [ -e "$f" ] && mv -i "$f" "$TRASH/$(basename "$f")_$(date +%s%N)"; done ;;
lr) ls -la "$TRASH" ;;
ur)
shift
for f; do [ -e "$TRASH/$f" ] && mv -i "$TRASH/$f" "$(dirname "$f")"; done ;;
cls)
read -p "Empty trash? (y/N) " c && [ "$c" = "y" ] && rm -rf "$TRASH"/* ;;
*) echo "Usage: $0 {delete|lr|ur|cls} [files...]"; exit 1 ;;
esac
赋予执行权限并加入 PATH,便于随处调用。find "$HOME/.trash" -type f -mtime +30 -delete;可按需改为目录、加日志与 dry-run 先行验证。三 多用户与根分区场景
/etc/cron.daily/trash-cleanup),集中执行 trash-empty --age 7d,避免个别用户回收站膨胀。四 快速应急与日常维护
rm -rf ~/.local/share/Trash/files/*
rm -rf ~/.local/share/Trash/info/*
在 GNOME 下也可用 gvfs-empty-trash --force 立即清空。df -h
du -sh ~/.local/share/Trash
sudo rm -rf 清理回收站,以免误删系统文件;重要数据务必先备份;在别名或脚本中对关键路径加保护,减少风险。