ubuntu

Ubuntu Recycle 怎样设置容量限制

小樊
51
2025-07-15 03:26:02
栏目: 智能运维

在Ubuntu系统中,设置回收站容量限制通常涉及到文件系统的配额管理。以下是一些常见的方法:

方法一:使用quota工具

  1. 安装quota工具
sudo apt-get install quota # Debian/Ubuntu
sudo yum install quota # CentOS/RHEL
  1. 启用quota: 编辑 /etc/fstab 文件,为需要设置配额的分区添加 usrquotagrpquota 选项。例如:
/dev/sda1 /home ext4 defaults,usrquota,grpquota 1 2

然后重新挂载分区:

sudo mount -o remount /home
  1. 初始化quota数据库
sudo quotacheck -cum /home
  1. 启动quota服务
sudo quotaon -vug /home
  1. 设置用户配额: 使用 edquota 命令为用户设置配额。例如:
sudo edquota -u username

在编辑器中设置软限制和硬限制:

Disk quotas for user username (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/sda1 102400 50000 60000 1000 2000 2500

方法二:使用flock和自定义脚本

如果你不想使用quota工具,可以编写一个自定义脚本来监控和限制回收站的容量。

  1. 创建监控脚本
#!/bin/bash
RECYCLE_BIN="/path/to/recycle/bin"
MAX_SIZE=104857600 # 100MB in bytes
while true; do
    total_size=$(du -sb "$RECYCLE_BIN" | awk '{print $1}')
    if [ "$total_size" -gt "$MAX_SIZE" ]; then
        # Remove oldest files until size is within limit
        find "$RECYCLE_BIN" -type f -printf '%T@ %p
' | sort -n | cut -d ' ' -f 2- | head -n 1 | xargs rm -rf
    fi
    sleep 60
done
  1. 设置脚本权限并运行
chmod +x /path/to/monitor_script.sh
nohup /path/to/monitor_script.sh &

方法三:使用trash-cli和cron

如果你使用的是 trash-cli 来管理回收站,可以结合 cron 任务来定期清理。

  1. 安装trash-cli
sudo apt-get install trash-cli # Debian/Ubuntu
sudo yum install trash-cli # CentOS/RHEL
  1. 创建清理脚本
#!/bin/bash
TRASH_DIR="$HOME/.local/share/Trash/files"
MAX_SIZE=104857600 # 100MB in bytes
total_size=$(du -sb "$TRASH_DIR" | awk '{print $1}')
if [ "$total_size" -gt "$MAX_SIZE" ]; then
    # Remove oldest files until size is within limit
    find "$TRASH_DIR" -type f -printf '%T@ %p
' | sort -n | cut -d ' ' -f 2- | head -n 1 | xargs rm -rf
fi
  1. 设置脚本权限并添加到cron
chmod +x /path/to/cleanup_script.sh
crontab -e

添加以下行以每小时运行一次清理脚本:

0 * * * * /path/to/cleanup_script.sh

0
看了该问题的人还看了