Written by razrlele
18:13 February 4, 2015
将下列代码依次保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#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 |
1 2 3 4 5 6 7 8 |
#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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#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 |
1 2 3 4 5 6 7 8 9 10 |
#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 |
赋予脚本执行权限
1 |
chmod +x delete restoreTrash logTrashDir cleanTrashCan |
delete脚本执行过程
回收站目录
生成.log记录
restoreTrash 脚本执行过程
最后再用systemd/Timers定期每星期日下午六点执行cleanTrashCan脚本 首先编辑
1 |
/etc/systemd/system/cleanTrash.service |
1 2 3 4 5 6 |
[Unit] Description=Clean Trash Can [Service] Type=simple ExecStart=/bin/cleanTrashCan |
再编辑
1 |
/etc/systemd/system/cleanTrash.timer |
1 2 3 4 5 6 7 8 9 |
[Unit] Description=Runs cleanTrashCan every week [Timer] OnCalendar=Sun, 18:00 Unit=cleanTrash.service [Install] WantedBy=multi-user.target |
执行
1 2 |
systemctl start cleanTrash.timer systemctl enable cleanTrash.timer |
References: