您的位置:首页 > 运维架构 > Linux

回收站linux实现

2015-08-20 22:44 573 查看
#Save as /bin/delete
#!/bin/bash
realrm="/bin/rm"
if [ ! -d ~/trash ]
then
mkdir -v ~/trash
chmod 777 ~/trash
fi

if [ $# -eq 0 ]
then
echo "Usage: delete file1 [file2 file3....]"
echo "If the options contain -f, then the script will exec 'rm' directly"
fi

while getopts "dfiPRrvw" opt
do
case $opt in
f)
exec $realrm "$@"
;;
*)
# do nothing
;;
esac
done

echo -ne "Are you sure you want to move the files to the trash?[Y/N]:\a"
read reply
if [ $reply = "y" -o $reply = "Y" ]
then #####

for file in $@
do
if [ -f "$file" -o -d "$file" ]
then
if [ -f "$file" ] && [ `ls -l $file|awk ' {print $5}'` -gt 2147483648 ]
then
echo "$file size is larger than 2G, will be deleted directly"
`rm -rf $file`
elif [ -d "$file" ] && [ `du -sb $file|awk '{print $1}'` -gt 2147483648]
then
echo "The directory:$file is larger than 2G, will be deleted directly"
`rm -rf $file`
fi
fi
done
fi
now=`date +%Y%m%d_%H_%M_%S`
filename="${file##*/}"
newfilename="${file##*/}_${now}"
mark1="."
mark2="/"
if [ "$file" = ${file/$mark2} ]
then
fullpath="$(pwd)/$file"
elif [ "$file" != ${file/$mark1} ]
then
fullpath="$(pwd)${file/$mark1}"
else
fullpath="$file"
fi

echo "the full path of this file is : $fullpath"
if mv -f $file ~/trash/$newfilename
then
$(/bin/logTrashDir "$newfilename $filename $now $fullpath")
echo "files: $file is deleted"
else
echo "the operation is failed"
fi

#Save as /bin/logTrashDir
#!/bin/bash
if [ ! -f ~/trash/.log ]
then
touch ~/trash/.log
chmod 700 ~/trash/.log
fi
echo $1 $2 $3 $4>> /home/razrlele/trash/.log

#Save as /bin/restoreTrash
#!/bin/bash
originalPath=$(awk /$filename/'{print $4}' "$HOME/trash/.log")

filenameNow=$(awk /$filename/'{print $1}' "$HOME/trash/.log")
filenamebefore=$(awk /$filename/'{print $2}' "$HOME/trash/.log")
echo "you are about to restore $filenameNow,original name is $filenamebefore"
echo "original path is $originalPath"

echo "Are you sure to do that?[Y/N]"
read reply
if [ $reply = "y" ] || [ $reply = "Y" ]
then
$(mv -b "$HOME/trash/$filenameNow" $originalPath)
$(sed -i /$filenameNow/'d' "$HOME/trash/.log")
else
echo "no files restored"
fi

#Save as /bin/cleanTrashCan
#!/bin/bash
arrayA=($(find ~/trash/* -mtime +7 | awk '{print $1}'))
for file in ${arrayA[@]}
do
$(rm -rf "${file}")
filename="${file##*/}"
echo $filename
$(sed -i /$filename/'d' "$HOME/trash/.log")
done


赋予脚本执行权限
chmod +x delete restoreTrash logTrashDir cleanTrashCan


delete脚本执行过程





回收站目录





生成.log记录





restoreTrash 脚本执行过程





最后再用systemd/Timers定期每星期日下午六点执行cleanTrashCan脚本 首先编辑
/etc/systemd/system/cleanTrash.service

[Unit]
Description=Clean Trash Can

[Service]
Type=simple
ExecStart=/bin/cleanTrashCan


再编辑
/etc/systemd/system/cleanTrash.timer

[Unit]
Description=Runs cleanTrashCan every week

[Timer]
OnCalendar=Sun, 18:00
Unit=cleanTrash.service

[Install]
WantedBy=multi-user.target


执行
systemctl start cleanTrash.timer
systemctl enable cleanTrash.timer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: