shell编程_实现回收站,避免误删文件

Posted wang-li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell编程_实现回收站,避免误删文件相关的知识,希望对你有一定的参考价值。

代码如下:

下载:https://gitee.com/LoongWang/shellrecycling.git

 1 #/usr/bin/env bash
 2 
 3 
 4 #定义
 5 #定义回收站目录
 6 RecyclingDir=/usr/local/recycling/
 7 #定义回收站log文件
 8 RecyclingLog=/var/log/recyclinglog
 9 #定义回收站家目录
10 UserRecyclingDir="/usr/local/recycling/$(whoami)"
11 #定义返回码
12 ReturnCode=0
13 
14 #程序开始
15 #检测主文件是否存在、没有则新建
16 
17 for DirName in $RecyclingDir $UserRecyclingDir
18 do
19     if [ ! -d $DirName ]
20     then
21         #将Log写入文件中
22         echo "$(date +"%F %H:%M:%S") - Make Dirctory" >> $RecyclingLog
23         #创建目录
24         mkdir $DirName
25     
26         if [ $? -eq 0 ]
27         then
28             echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Ok" >> $RecyclingLog
29         else
30             echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Fail" >> $RecyclingLog
31             echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail"
32  
33             ReturnCode=127
34             echo -e "The Linux Comamnd is error , returncode:127\n"
35             echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail"
36             echo "Your can see log the $RecyclingLog" 
37 
38             exit $ReturnCode
39         fi
40 
41     fi
42 done
43 
44 #移动目录至回收站
45 if [ $# -eq 0 ]
46 then
47     echo "Usage:$0 file1 file2 file3 ..."    
48     ReturnCode=1
49 else
50     #循环开始
51     for DelFile in [email protected]
52     do
53         #判断是否存在文件
54         if [ -e $DelFile ]
55         then
56             echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile" >> $RecyclingLog
57             mv $DelFile $UserRecyclingDir
58             
59             #判断命令是否执行成功
60             if [ $? -eq 0 ]
61             then
62                 #命令执行成功
63                 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - OK" >> $RecyclingLog
64                 echo -e "$(date +"%F %H:%M:%S") - Delete $DelFile - OK"
65             else
66                 #命令执行失败
67                 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" >> $RecyclingLog
68                 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" 
69                 ReturnCode=1
70             fi
71         else
72             #不存在文件
73             echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" >> $RecyclingLog
74             echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" 
75             ReturnCode=1
76         fi
77     done
78     
79     exit $ReturnCode
80 fi

 

以上是关于shell编程_实现回收站,避免误删文件的主要内容,如果未能解决你的问题,请参考以下文章

为rm -f 添加回收站功能 防止误删

Windows误删文件如何恢复(Shift+Delete清空回收站)

Windows误删文件如何恢复(Shift+Delete清空回收站)

Windows误删文件如何恢复(Shift+Delete清空回收站)

Windows误删文件如何恢复(Shift+Delete清空回收站)

想要恢复回收站误删文件,就用EasyRecovery!