Shell脚本实现Linux回收站功能

在系统运维过程中“Linux系统上常见的误操作”之首就是删除rm操作(特别是rm -rf),这些被误删的文件将无法被取回,那么是否有办法解决这个问题呢?目前有两种解决方案:

1. 第一种是如果你在误删除文件以后,若没有对硬盘做大量的IO(也就是硬盘上存放该被删除文件的设备块没被新的数据所覆盖),那么可借助一些第三方软件支持从硬盘上直接还原文件,在Linux有这方面的第三方软件,类似windows平台下的FinalData。

2. 第二种则是在你每次删除文件时,都将被删除的文件复制一份到指定的目录下,在你发现误删文件后能够从该目录下取回数据,这就相当于windows下的回收站了。

这篇文章将介绍的则是第二种解决方案,其优缺点都是非常明显的,优点是:可以确保每次删除的文件都能够找回;缺点就是:需要耗费相当一部分硬盘空间来存放被删除的文件;同时需要在每次删除文件时都必须通过这个删除脚本来处理。

下面看看这个Linux Shell脚本,既然其优缺点都非常明显,但这妨碍我们学习这种解决问题的思路……

#!/bin/bashfrom1=<span class="katex math inline">1from2=</span>2garbage=<span class="katex math inline">HOME/.garbagemvlog=</span>garbage/mv.logif [ ! -e <span class="katex math inline">garbage ] then    mkdir -p</span>garbage    chmod 777 <span class="katex math inline">garbagefifunction rand{a=(0 1 2 3 4 5 6 7 8 9 a b c d e A B C D E F )for ((i=0;i></span>mvlog   mv "<span class="katex math inline">from1"  "</span>garbage/<span class="katex math inline">from1:</span>random"fi}function more{for file in *doecho "`pwd`/:<span class="katex math inline">file:</span>random:`date`" >> <span class="katex math inline">mvlogmv</span>file "<span class="katex math inline">garbage/</span>file:<span class="katex math inline">random"done 2> /dev/null}function rmi{if [ ! -d "</span>from2" ] then   echo -n "rm:remove regular empty file ‘<span class="katex math inline">from2′?" ; read answer;    if [ "</span>answer" = ‘y’ -o "<span class="katex math inline">answer" = ‘Y’ ]     then        echo "`pwd`/:</span>from2:<span class="katex math inline">random:`date`" >></span>mvlog         mv "<span class="katex math inline">from2" "</span>garbage/<span class="katex math inline">from2:</span>random"    fielse   echo "rm: cannot remove directory ‘<span class="katex math inline">from2′: Is a directory"fi}function rmf{if [ ! -d "</span>from2" ] then        echo "`pwd`/:<span class="katex math inline">from2:</span>random:`date`" >> <span class="katex math inline">mvlog         mv "</span>from2" "<span class="katex math inline">garbage/</span>from2:<span class="katex math inline">random"else   echo "rm: cannot remove directory ‘</span>from2′: Is a directory"fi}function rmr{if [ -e "<span class="katex math inline">from2" ] then        result=</span>(echo <span class="katex math inline">from2 | sed ‘s/////g’)        echo "`pwd`/:</span>result:<span class="katex math inline">random:`date`" >></span>mvlog         mv "<span class="katex math inline">result" "</span>garbage/<span class="katex math inline">result:</span>random"fi}function rml{while :doclearline=<span class="katex math inline">(cat -n</span>mvlog | awk -F : ‘{print <span class="katex math inline">1,"FileName:"</span>2,    "Time:"<span class="katex math inline">4}’)linecount=</span>(cat <span class="katex math inline">mvlog | wc -l)echo -e "</span>line/c"echoechoecho "Please input number you want revent(line count:<span class="katex math inline">linecount)–exit(e)"read answer if [ "</span>answer" = e -o "<span class="katex math inline">answer" = E ]  then    break else    (     echo "please input y(sure:)"       read answer1       if [ "</span>answer1" = y -o "<span class="katex math inline">answer" = Y ]        then          address=</span>(sed -n "<span class="katex math inline">answer""p"</span>mvlog | awk -F : ‘{print <span class="katex math inline">1}’)          filename=</span>(sed -n "<span class="katex math inline">answer""p"</span>mvlog | awk -F : ‘{print <span class="katex math inline">2}’)          filerand=</span>(sed -n "<span class="katex math inline">answer""p"</span>mvlog | awk -F : ‘{print <span class="katex math inline">3}’)          fullname=</span>address<span class="katex math inline">filename           if [ -e "</span>fullname" ]            then               echo "The file exist!"               sleep 1           else               old="<span class="katex math inline">garbage/</span>filename:<span class="katex math inline">filerand"               new="</span>address<span class="katex math inline">filename"               mv "</span>old" "<span class="katex math inline">new"               delline=</span>( cat <span class="katex math inline">mvlog | sed "</span>answer""d" | sort -o <span class="katex math inline">mvlog)               echo "update ok!!!"               sleep 1           fi       fi    )  fidone}function help{echo "-i)  If you wants delete some file , this function is confirm you want,the same as old rm.-f)  If you wants delete some directory ,you can use this function ,the same as old rm.-r)  If you wants delete some directory of file ,this function can use , the same as old rm.-l)  This is new function,is you wants resume some file or directory you can use this function,     first this function can list some file in you garbage , these have some number ,if you     wants resume 1,you can input 1 and then input y to confirm.If you want add some function or some new idear please contact me…     author:wds      email:7717060@sina.com"}case "</span>1"  in[a-z]) : ;;[0-9]) : ;;[A-Z]) : ;;    ?) more ;;    *) :;;esacif [ "<span class="katex math inline">#" -eq 0 ] then   echo -n "rm: missing operandTry ‘rm –help’ for more informaction."fiif [ "</span>#" -eq 1 ] then   case "<span class="katex math inline">from1"      in       -i) echo "Try ‘rm –help’ for more informaction."; break ;;       -f) echo "Try ‘rm –help’ for more informaction."; break ;;       -r) echo "Try ‘rm –help’ for more informaction."; break ;;       -l) rml ;;   –help) help;;        *) rm1;;   esacfiif [ "</span>#" -eq 2 ] then    case "<span class="katex math inline">from1"      in       -i) rmi ;;       -f) rmf ;;       -r) rmr ;;       -l) rml ;;      -rf) rmr ;;   –help) help ;;    esacfi  if [ "</span>#" -gt 2 ]         then           for file in <span class="katex math inline">*             do               mv</span>file "$home/"           done 2> /dev/null        fi

本文链接:http://www.yunweipai.com/1579.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/53180.html

(0)
上一篇 2021年8月6日 18:12
下一篇 2021年8月6日 18:12

相关推荐

发表回复

登录后才能评论