
最近注意了一下自己的Crontab任务,发觉原来以前的东西在Xfce桌面上已经不适用了--不能仅仅通过删除 Trash/files (~/.local/share/Trash/files/)
同时删除 Trash/info (~/.local/share/Trash/info/) 里面的文件了。本打算写一个根据只要
代码: 全选
find ~/.local/share/Trash/info/ -a -ctime +30
但发觉原来 free desktop 中已经有一个开源的 回收站命令行接口 (see more in Git trash-cli) ,可以让我们通过命令行进行 文件回收站 操作。其中
代码: 全选
trash-empty [文件删除至现在的天数] :清空回收站
节省时间,就利用这个接口来实现 自动管理清除回收站 的想法吧!
首先安装 trash-cli
ubuntu 用户可以直接
代码: 全选
sudo aptitude install trash-cli
或者 可以到 Git trash-cli 下载源码安装,方法如下
代码: 全选
# grab the latest sources
wget https://github.com/andreafrancia/trash-cli/tarball/master
# unpack and install
tar xfz andreafrancia-trash-cli-xxxxxxxx.tar.gz
cd andreafrancia-trash-cli-xxxxxxxx
sudo python setup.py install
终端输入
代码: 全选
crontab -e
Crontab运行时间用法详细见Linux下的定时任务crontab设置,最后记得把 标准输出与错误输出给屏蔽掉!
代码: 全选
##我的Crontab范例:
# 自动清除删除至现在 15 天的回收站文件
# 每天运行三次: 9:15/12:50/22:50 (包含着一天中可能开启的时间)
# 标准输出与错误输出都屏蔽掉
50 9 * * * auto-empty 15 >/dev/null 2>/dev/null
50 12 * * * auto-empty 15 >/dev/null 2>/dev/null
50 22 * * * auto-empty 15 >/dev/null 2>/dev/null
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------重要的修正线

代码: 全选
find ~/.local/share/Trash/files/ -maxdepth 1 -a -ctime +30 | xargs rm -rf
不要-maxdepth 1后,就会递归进入下层文件夹查找。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------重要的修正线

代码: 全选
find /home/$USER/.local/share/Trash/files -depth ! -path '/home/$USER/.local/share/Trash/files' -a -ctime +30 -delete
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------以前分割线

大家可以随意使用并转发这段code
[Preset]
这段bash可以自动除回收站中的过期文件或者文件夹:
初始设定为
- 过期天数(自删除文件那天起的天数)=30天
- 回收站文件夹位置=$HOME/.local/share/Trash/files
- 回收站信息文件夹(包含有文件的删除时间戳)=$HOME/.local/share/Trash/info
代码: 全选
#!/bin/bash -
# Remove 30-days-ago files in Trash regularly.
# It runs in Contab every three day.
#
# !Be carefull to use this script.
#
# by ekeyme 09-21-2010
# IFS='\ \n\t'
IFS='
'
PATH=/usr/local/bin:/bin:/usr/bin
export PATH
# Time interval defaul: 30days
SET_inter=$((30*24*60*60))
# Timetmp now.
nowt=$(date +%D)
nowt_tmp=$(date -d $nowt +%s)
# Trash directory--Infomation dir. : Files dir.
Tr_infodir="$HOME/.local/share/Trash/info"
Tr_filesdir="$HOME/.local/share/Trash/files"
cd "$Tr_infodir" || exit
FILESNAME=$(ls -a | grep -E -v '^\.$|^\.\.$')
# Set the IFS='\n\t' for keyword 'for', some filename contains space-key.
IFS='
'
for filename in $FILESNAME
do
# Get the files in Trash.
Rfilename=$(sed -e 's/\.trashinfo$//' <<< "$filename")
file="$Tr_filesdir/$Rfilename"
# Get the files Deletion Time.
delfilet=$(date +%D -d $(cat "$filename" |
sed -n '/DeletionDate=/ s/DeletionDate=//p'))
delfilet_tmp=$(date -d $delfilet +%s)
# Comparea and rm file...
interval=$(($nowt_tmp - $delfilet_tmp))
if [[ $interval -ge $SET_inter ]]; then
rm -rf $file
fi
done