shell实现linux回收站的功能
下载:
https://gitee.com/LoongWang/shellrecycling/repository/archive/master.zip
代码:
#/usr/bin/env bash #定义 #定义回收站目录 RecyclingDir='/usr/local/recycling/' #定义回收站log文件夹 RecyclingLogDir='/var/log/recyclinglog/' #定义回收站log文件 RecyclingLog="/var/log/recyclinglog/$(whoami)" #定义回收站家目录 UserRecyclingDir="/usr/local/recycling/$(whoami)" #定义返回码 ReturnCode= #程序开始 #检测主文件是否存在、没有则新建 for DirName in $RecyclingLogDir $RecyclingDir $UserRecyclingDir do if [ ! -d $DirName ] then if [[ $DirName == "$RecyclingLogDir" ]] then mkdir $RecyclingLogDir $RecyclingLogDir continue fi #将Log写入文件中 echo "$(date +"%F %H:%M:%S") - Make Dirctory" >> $RecyclingLog $RecyclingLog #创建目录 mkdir $DirName ] then echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Ok" >> $RecyclingLog else echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Fail" >> $RecyclingLog echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail" ReturnCode= echo -e "The Linux Comamnd is error , returncode:127\n" echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail" echo "Your can see log the $RecyclingLog" exit $ReturnCode fi $DirName if [[ $DirName == $UserRecyclingDir ]] then $DirName fi fi done #移动目录至回收站 ] then echo "Usage:$0 file1 file2 file3 ..." ReturnCode= else #循环开始 for DelFile in $@ do #判断是否存在文件 if [ -e $DelFile ] then echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile" >> $RecyclingLog mv $DelFile $UserRecyclingDir/$(date +"%F:%H:%M:%S")-$DelFile #判断命令是否执行成功 ] then #命令执行成功 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - OK" >> $RecyclingLog echo -e "$(date +"%F %H:%M:%S") - Delete $DelFile - OK" else #命令执行失败 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" >> $RecyclingLog echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" ReturnCode= fi else #不存在文件 echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" >> $RecyclingLog echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" ReturnCode= fi done exit $ReturnCode fi
#!/usr/bin/env bash RecyclingDir="/usr/local/recycling" AudoDelRecyclingLog="/var/log/auto_del_recycling.log" errorlog="error.log" ] then for DirName in `ls $RecyclingDir` do if [ -d $RecyclingDir\/$DirName ] then for FileName in `ls $RecyclingDir\/$DirName` do NowDate=$(date +"%s") GetFile=$(stat -c %X $RecyclingDir\/$DirName\/$FileName) DifferTime=$(echo "value=0;$NowDate - $GetFile" | bc) ] then echo -e "$(date +"%F_%H:%M:%S")-Deleteing $RecyclingDir $DirName(type $DirName is:$(file $RecyclingDir\/$DirName\/$FileName))" >> $AudoDelRecyclingLog rm -rf $RecyclingDir\/$DirName\/$FileName echo -e "$(date +"%F_%H:%M:%S")-Deleted $RecyclingDir $DirName $FileName OK" >> $AudoDelRecyclingLog fi done else echo -e "$(date +"%F_%H:%M:%S")-$DirName is not dirctory(type $DirName is:$(file $RecyclingDir\/$DirName))" >> $AudoDelRecyclingLog fi done else echo -e "$(date +"%F_%H:%M:%S")-The User ID is not root,please login root exec(uid:`id -u`)" >> $errorlog fi