shell实现linux回收站的功能
下载:
https://gitee.com/LoongWang/shellrecycling/repository/archive/master.zip
代码:
1 #/usr/bin/env bash
2
3 #定义
4 #定义回收站目录
5 RecyclingDir='/usr/local/recycling/'
6 #定义回收站log文件夹
7 RecyclingLogDir='/var/log/recyclinglog/'
8 #定义回收站log文件
9 RecyclingLog="/var/log/recyclinglog/$(whoami)"
10 #定义回收站家目录
11 UserRecyclingDir="/usr/local/recycling/$(whoami)"
12 #定义返回码
13 ReturnCode=0
14
15 #程序开始
16 #检测主文件是否存在、没有则新建
17
18 for DirName in $RecyclingLogDir $RecyclingDir $UserRecyclingDir
19 do
20 if [ ! -d $DirName ]
21 then
22 if [[ $DirName == "$RecyclingLogDir" ]]
23 then
24 mkdir $RecyclingLogDir
25 chmod 777 $RecyclingLogDir
26 continue
27 fi
28 #将Log写入文件中
29 echo "$(date +"%F %H:%M:%S") - Make Dirctory" >> $RecyclingLog
30 chmod 700 $RecyclingLog
31 #创建目录
32 mkdir $DirName
33
34 if [ $? -eq 0 ]
35 then
36 echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Ok" >> $RecyclingLog
37 else
38 echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Fail" >> $RecyclingLog
39 echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail"
40
41 ReturnCode=127
42 echo -e "The Linux Comamnd is error , returncode:127\n"
43 echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail"
44 echo "Your can see log the $RecyclingLog"
45
46 exit $ReturnCode
47 fi
48 chmod 777 $DirName
49
50 if [[ $DirName == $UserRecyclingDir ]]
51 then
52 chmod 700 $DirName
53 fi
54
55 fi
56 done
57
58 #移动目录至回收站
59 if [ $# -eq 0 ]
60 then
61 echo "Usage:$0 file1 file2 file3 ..."
62 ReturnCode=1
63 else
64 #循环开始
65 for DelFile in $@
66 do
67 #判断是否存在文件
68 if [ -e $DelFile ]
69 then
70 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile" >> $RecyclingLog
71
72 mv $DelFile $UserRecyclingDir/$(date +"%F:%H:%M:%S")-$DelFile
73
74 #判断命令是否执行成功
75 if [ $? -eq 0 ]
76 then
77 #命令执行成功
78 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - OK" >> $RecyclingLog
79 echo -e "$(date +"%F %H:%M:%S") - Delete $DelFile - OK"
80 else
81 #命令执行失败
82 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" >> $RecyclingLog
83 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail"
84 ReturnCode=1
85 fi
86 else
87 #不存在文件
88 echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" >> $RecyclingLog
89 echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists"
90 ReturnCode=1
91 fi
92 done
93
94 exit $ReturnCode
95 fi
1 #!/usr/bin/env bash
2
3 RecyclingDir="/usr/local/recycling"
4 AudoDelRecyclingLog="/var/log/auto_del_recycling.log"
5 errorlog="error.log"
6
7 if [ `id -u` -eq 0 ]
8 then
9 for DirName in `ls $RecyclingDir`
10 do
11 if [ -d $RecyclingDir\/$DirName ]
12 then
13 for FileName in `ls $RecyclingDir\/$DirName`
14 do
15
16 NowDate=$(date +"%s")
17 GetFile=$(stat -c %X $RecyclingDir\/$DirName\/$FileName)
18 DifferTime=$(echo "value=0;$NowDate - $GetFile" | bc)
19
20 if [ $DifferTime -gt 2592000 ]
21 then
22 echo -e "$(date +"%F_%H:%M:%S")-Deleteing $RecyclingDir $DirName(type $DirName is:$(file $RecyclingDir\/$DirName\/$FileName))" >> $AudoDelRecyclingLog
23 rm -rf $RecyclingDir\/$DirName\/$FileName
24 echo -e "$(date +"%F_%H:%M:%S")-Deleted $RecyclingDir $DirName $FileName OK" >> $AudoDelRecyclingLog
25 fi
26 done
27
28
29 else
30 echo -e "$(date +"%F_%H:%M:%S")-$DirName is not dirctory(type $DirName is:$(file $RecyclingDir\/$DirName))" >> $AudoDelRecyclingLog
31 fi
32 done
33 else
34 echo -e "$(date +"%F_%H:%M:%S")-The User ID is not root,please login root exec(uid:`id -u`)" >> $errorlog
35 fi