Ubuntu Recycle(回收站)存储限制设置方法
Ubuntu的Recycle(回收站)功能默认无图形化容量设置选项,但可通过图形工具、配置文件修改或自动化脚本实现存储限制。以下是具体方法:
适用场景:偏好图形界面的用户,快速配置回收站大小。
步骤:
sudo apt update && sudo apt install dconf-editor
dconf-editor并回车,或在应用菜单中搜索“dconf Editor”启动。org→gnome→desktop→nautilus→trash。max-size选项(单位:字节),双击编辑。例如,设置1GB上限需输入1073741824(1GB=1024×1024×1024字节)。适用场景:无需安装额外工具,直接编辑系统配置文件。
步骤:
nano)打开用户级Nautilus配置文件:nano ~/.config/nautilus/desktop-metadata
[Trash]
max-size=1073741824
Ctrl+O保存文件,Ctrl+X退出编辑器。nautilus -q
(注:若Nautilus未自动重启,可注销并重新登录。)适用场景:需要动态监控回收站大小,自动清理旧文件(推荐)。
步骤:
sudo apt update && sudo apt install trash-cli
clean_recycle.sh):#!/bin/bash
RECYCLE_DIR="$HOME/.local/share/Trash/files"
MAX_SIZE=$((100 * 1024 * 1024)) # 100MB(单位:字节)
# 获取当前回收站大小
CURRENT_SIZE=$(du -sb "$RECYCLE_DIR" | awk '{print $1}')
# 若超过限制,删除最旧的文件
if [ "$CURRENT_SIZE" -gt "$MAX_SIZE" ]; then
echo "回收站大小超过限制(${CURRENT_SIZE}B > ${MAX_SIZE}B),开始清理..."
find "$RECYCLE_DIR" -type f -printf '%T@ %p\n' | sort -n | cut -d' ' -f2- | head -n 10 | xargs rm -f
# 可选:同时清理对应的info文件(避免残留)
find "$HOME/.local/share/Trash/info" -type f -printf '%T@ %p\n' | sort -n | cut -d' ' -f2- | head -n 10 | xargs rm -f
fi
chmod +x clean_recycle.sh
crontab -e,添加以下行:0 1 * * * /path/to/clean_recycle.sh
步骤:
monitor_trash.sh):#!/bin/bash
RECYCLE_DIR="$HOME/.local/share/Trash/files"
MAX_SIZE=$((100 * 1024 * 1024)) # 100MB
CURRENT_SIZE=$(du -sb "$RECYCLE_DIR" | awk '{print $1}')
if [ "$CURRENT_SIZE" -gt "$MAX_SIZE" ]; then
echo "回收站大小超过限制,清理中..."
rm -rf "$RECYCLE_DIR"/*
fi
chmod +x monitor_trash.sh
./monitor_trash.sh
(注:若需长期运行,可将脚本放入后台或添加到开机启动。)适用场景:需要限制特定用户或组的回收站总使用量(高级用法)。
步骤:
sudo apt update && sudo apt install quota
/etc/fstab,在目标分区(如/home)挂载选项中添加usrquota:/dev/sda1 /home ext4 defaults,usrquota 1 2
sudo mount -o remount /home
sudo quotacheck -cum /home
sudo quotaon -vug /home
sudo edquota -u username
在编辑器中设置soft(软限制,如500MB)和hard(硬限制,如600MB)值,保存后生效。