CentOS設置Recycle(回收站)的常見方法
CentOS系統默認未啟用圖形化回收站功能,但可通過以下方法實現類似效果,保障數據安全:
trash-cli是命令行工具,可將文件移動至回收站而非直接刪除,支持設置大小、保留時間等策略。
sudo yum install trash-cli
。trash /path/to/file_or_directory
(替代原生rm
命令);trash-list
;trash-empty
(可指定天數,如trash-empty 7
清理7天前的文件);trash-restore
(按提示選擇需恢復的文件)。/etc/trash.conf
(全局)或~/.config/trash.conf
(用戶級),調整以下參數:[Trash]
trash_dir = ~/.local/share/Trash # 回收站目錄(默認路徑)
trash_max_file_size = 10485760 # 單個文件最大大?。?0MB)
trash_max_files = 1000 # 回收站最大文件數量
trash_keep_days = 7 # 文件保留天數(7天后自動刪除)
btrfs支持內置回收站,需通過掛載選項啟用:
/dev/sda1 /mnt/data btrfs defaults 1 1
),添加discard,subvol=@
選項,改為:/dev/sda1 /mnt/data btrfs defaults,discard,subvol=@ 1 1
。sudo mount -o remount /mnt/data
使配置生效。btrfs subvolume list /mnt/data
(回收站通常為@trash
子卷);btrfs subvolume find-new
查找并復制文件至原路徑;btrfs subvolume delete /mnt/data/@trash
。ext4、xfs等文件系統需依賴第三方工具(如trash-cli)實現回收站功能,配置方法同“1. 使用trash-cli工具”。
若需更貼合需求的回收站,可編寫腳本實現:
mkdir -p ~/.trash
(用戶級)或/data/Recycle
(系統級)。/usr/local/bin/recycle
):#!/bin/bash
RECYCLE_DIR="$HOME/.trash"
TIMESTAMP=$(date +%s)
for item in "$@"; do
mv "$item" "$RECYCLE_DIR/$(basename "$item")_$TIMESTAMP"
done
chmod +x /usr/local/bin/recycle
;rm
命令:編輯~/.bashrc
,添加alias rm='recycle'
,然后執行source ~/.bashrc
。crontab -e
添加0 2 * * * find ~/.trash -type f -mtime +30 -delete
)定期清理30天前的文件。無論使用哪種方法,均需防范回收站占用過多磁盤空間:
/usr/local/bin/clean_recycle.sh
,內容如下:#!/bin/bash
RECYCLE_BIN="$HOME/.local/share/Trash/files"
MAX_SIZE=$((10 * 1024 * 1024)) # 10GB(單位:KB)
TOTAL_SIZE=$(du -sk "$RECYCLE_BIN" | awk '{print $1}')
if [ "$TOTAL_SIZE" -gt "$MAX_SIZE" ]; then
find "$RECYCLE_BIN" -type f -delete
fi
crontab -e
添加0 3 * * * /usr/local/bin/clean_recycle.sh
,每天凌晨3點執行清理。以上方法可根據系統環境(如是否使用圖形界面)、需求(如是否需要圖形化操作)選擇使用。其中,trash-cli是最通用且功能完善的解決方案,適用于大多數場景。