回收站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脚本执行过程
回收站目录
生成.log记录
restoreTrash 脚本执行过程
最后再用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
- 回收站linux实现
- linux 实现回收站功能
- 为linux系统实现回收站
- Linux下回收站的设计与实现
- 回收站功能在 Linux 中的实现
- 回收站功能在 Linux 中的实现
- Linux删除文件实现回收站功能
- Linux回收站的设计与实现
- 在LINUX上实现 windows 回收站功能的脚本
- linux下默认删除文件到回收站(bash实现)
- 回收站功能实现
- linux 下清空回收站命令
- Linux 清空回收站
- linux 下清空回收站命令
- linux下清空回收站
- linux下清空回收站
- Linux垃圾回收站
- Linux回收站建立
- android studio使用中遇到的问题记录
- 真人行世,入火不热,沉水不溺
- SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- Android学习之Bitmap对象与字节数组相互转换
- 零基础学python-7.1 python中的字符串简介与常用函数
- 回收站linux实现
- 全面解析Linux数字文件权限
- 关于PHP发送邮件的两个类(找了好久......)
- Leetcode: Ugly Number
- 利害唯己,谁贵谁贱
- 关于树的一些基本算法
- 我的金工实习心得(五):金属工艺学实习(金工实习)小结
- 软件开发流程
- Core Data理论上初步了解