溫馨提示×

Linux如何設置回收站容量限制

小樊
55
2025-05-06 16:05:42
欄目: 智能運維

在Linux系統中,設置回收站容量限制通常涉及到文件系統的配額管理。以下是一些常見的方法:

方法一:使用quota工具

  1. 安裝quota工具

    sudo apt-get install quota  # Debian/Ubuntu
    sudo yum install quota      # CentOS/RHEL
    
  2. 啟用quota: 編輯/etc/fstab文件,為需要設置配額的分區添加usrquotagrpquota選項。

    /dev/sda1 /home ext4 defaults,usrquota,grpquota 1 2
    

    然后重新掛載分區:

    sudo mount -o remount /home
    
  3. 初始化quota數據庫

    sudo quotacheck -cum /home
    
  4. 啟動quota服務

    sudo quotaon -vug /home
    
  5. 設置用戶配額: 使用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\n' | sort -n | cut -d ' ' -f 2- | head -n 1 | xargs rm -rf
        fi
        sleep 60
    done
    
  2. 設置腳本權限并運行

    chmod +x /path/to/monitor_script.sh
    nohup /path/to/monitor_script.sh &
    

方法三:使用trash-clicron

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

  1. 安裝trash-cli

    sudo apt-get install trash-cli  # Debian/Ubuntu
    sudo yum install trash-cli      # CentOS/RHEL
    
  2. 創建清理腳本

    #!/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\n' | sort -n | cut -d ' ' -f 2- | head -n 1 | xargs rm -rf
    fi
    
  3. 設置腳本權限并添加到cron

    chmod +x /path/to/cleanup_script.sh
    crontab -e
    

    添加以下行以每小時運行一次清理腳本:

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

通過以上方法,你可以有效地設置和管理Linux系統中回收站的容量限制。選擇適合你需求的方法進行配置即可。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女