回收站linux实现

来源:互联网 发布:马自达睿翼轮毂数据 编辑:程序博客网 时间:2024/05/20 10:54
#Save as /bin/delete#!/bin/bashrealrm="/bin/rm"if [ ! -d ~/trash ]then    mkdir -v ~/trash    chmod 777 ~/trashfiif [ $# -eq 0 ]then    echo "Usage: delete file1 [file2 file3....]"    echo "If the options contain -f, then the script will exec 'rm' directly"fiwhile getopts "dfiPRrvw" opt    do        case $opt in            f)                exec $realrm "$@"                ;;            *)                # do nothing                ;;        esac    doneecho -ne "Are you sure you want to move the files to the trash?[Y/N]:\a"    read replyif [ $reply = "y" -o $reply = "Y" ]    then #####for file in $@ doif [ -f "$file" -o -d "$file" ]thenif [ -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 fidonefinow=`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"fiecho "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/bashif [ ! -f ~/trash/.log ]then     touch ~/trash/.log    chmod 700 ~/trash/.logfi    echo $1 $2 $3 $4>> /home/razrlele/trash/.log
#Save as /bin/restoreTrash#!/bin/bashoriginalPath=$(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/basharrayA=($(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=simpleExecStart=/bin/cleanTrashCan

再编辑

/etc/systemd/system/cleanTrash.timer
[Unit]Description=Runs cleanTrashCan every week[Timer]OnCalendar=Sun, 18:00Unit=cleanTrash.service[Install]WantedBy=multi-user.target

执行

systemctl start cleanTrash.timersystemctl enable cleanTrash.timer
0 0
原创粉丝点击