回收站linux实现

#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脚本执行过程

Screenshot from 2015-02-04 18:45:38

回收站目录

Screenshot from 2015-02-04 18:46:16

生成.log记录

Screenshot from 2015-02-04 18:46:33

restoreTrash 脚本执行过程

Screenshot from 2015-02-04 18:47:14

最后再用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值