现在的位置: 首页shell>正文
CentOS下shell脚本实现windows的回收站功能
2011年05月17日 shell 评论数 7 ⁄ 被围观 25,381 次+

脚本功能:
1、在用户执行rm命令时,将文件移动/dump-file/ ,被移动到dump-file 目录下的文件、文件夹以原文件名+删除除的日期格式存放,如 rm.sh 在删除后就会被自动移动到/dump-file/ 下并命名为rm.sh_2011-05-17_11:18:21.bak
2、此命令在执行时会检测文件或目录的大小,如果文件或目录超过2GB,将不会再将文件移动到回收站,而会直接删除,此功能是为了避免回收站过大而对系统造成影响

安装方法:

  1. cp -rp /bin/rm /bin/rm.bak   
  2. //将系统原有rm 命令改成rm.bak,记住 一定要改成rm.bak,因为回收站脚本需要调用 rm.bak这个命令。   
  3. cp -rp rm.sh /bin/rm   
  4. //将你的脚本文件rm.sh 覆盖成/bin/rm文件   
  5. chmod +x /bin/rm   
  6. //赋予/bin/rm可执行权限   
  7. echo "0 8  * * 6   /bin/sh  /fmnp/soft/clean-dump.sh " >>/var/spool/cron/tabs/root   
  8. //为了防止 /dump-file目录过大,采取每周进行一次删除操作,脚本如下:   
  9. #!/bin/bash   
  10. /bin/rm.bak -rf /dump-file/*   
  11. /bin/rm.bak -rf /tmp/*.rm-dump   

保存退出

  1. mkdir /dump-file && chmod -R 777 /dump-file   
  2. //创建/dump-file 目录并给予所有用户可写的权限。  

脚本如下:

  1. #!/bin/bash   
  2. DumpFile=/dump-file/   
  3. File=`echo $line|awk '{print $2}'`   
  4. aa=$(echo $1|grep "^-")   
  5. DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"  
  6.   
  7. function TestDir()   
  8. {   
  9.     Test_Dir=$(echo $line|grep "/"#test if is a directory.   
  10.     Test_Dir2=$(echo $line |awk '{print $2}'|grep "^/"# Test if the prameter starts with "/".   
  11.     Test_Dir3=$(echo $line |awk '{print $2}'|grep "^/tmp"#   
  12.     Dir2=$(echo $line |awk -F/ '{print $2}') #Extract the prameter after the first "/".   
  13.     Dir=$(echo $line |awk '{print $2}'|awk -F/ '{print $1}') ##Extract the prameter before the first "/".   
  14.     D1=`date +%F_%H:%M:%S.bak`   
  15.     Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`   
  16.     DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"  
  17.   
  18.     if [[ $Test_Dir != "" ]] ;then #If parameters include directory   
  19.         if [[ $Test_Dir2 != "" ]] ;then #If parameter starts with "/",as formate /a/b/c/   
  20.             if [[ $Test_Dir3 != "" ]];then # this test is for to repaire less command's error.   
  21.                 rm.bak -rf `echo $line|awk '{print $2}'`   
  22.             else  
  23.                 #echo $line "start with /"   
  24.                 mkdir $DumpFile$Dir2"_"$D1 2>/tmp/$DumpLog # Create directory as format /dump-file/(first directory after "/")   
  25.                           
  26.                 mv `echo $line|awk '{print $2}'` $DumpFile$Dir2"_"$D1  
  27.                 echo $DumpFile$Dir2"_"$D1  
  28.             fi   
  29.         else #Means that parameter starts without "/" ,as format a/b/c   
  30.             mkdir $DumpFile$Dir$D1 #Create directory as format /dump-file/(first directory before "/")   
  31.             mv `echo $line|awk '{print $2}'` $DumpFile$Dir$D1  
  32.             echo $line "starts before /"  
  33.         fi   
  34.     else  
  35.         Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`   
  36.         mv `echo $line|awk '{print $2}'` $DumpFile$Date  
  37.     fi   
  38. }   
  39.   
  40.   
  41. if [[ "$1" != "" ]] ;then #If the frist parameter is not empty.   
  42.     if [[ $aa != "$1" ]];then #If the first parameter not start with "-".   
  43.         du -s "$@" 2>/tmp/$DumpLog |while read line   
  44.         do  
  45.             size=`echo $line | awk '$1 >2000000{print $1}'`   
  46.             if [ "$size" > "1900000" ];then  
  47.                 #echo "Lager than 2Mb"   
  48.                 rm.bak -rf `echo $line|awk '{print $2}'`   
  49.             else  
  50.                 TestDir   
  51.             fi   
  52.         done   
  53.     else  
  54.         if [[ "$2" != ""  ]]; then  
  55.             until [ "$2" == ""  ]   
  56.             do  
  57.                  #echo "function is running"   
  58.                  du -s $2 2> /tmp/$DumpLog |while read line   
  59.                  do  
  60.                     size=`echo $line | awk '$1 >2000000{print $1}'`   
  61.                     if [ "$size" > "1900000" ];then  
  62.                         #echo "Lager than 2Mb"   
  63.                         rm.bak -rf $2  
  64.                     else  
  65.                         TestDir   
  66.   
  67.                     fi   
  68.                     shift   
  69.                  done   
  70.                  shift   
  71.             done   
  72.         else  
  73.             echo -e "No file detected./nTry 'rm --help' for more information. "  
  74.         fi   
  75.     fi   
  76. else  
  77.     echo -e "rm: missing operand /nTry 'rm --help' for more information."  
  78. fi  

rm

本文地址:http://www.92csz.com/39/673.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
 

目前有 7 条留言 其中:访客:5 条, 博主:2 条

  1. 无心无情 : 2011年05月17日17:11:49  -49楼

    博主你好,我是www.54zone.org的博主,我的域名被盗了,现在使用的是www.hyxl.org这个域名,麻烦改一下,谢谢~!

    • clairelume : 2011年05月17日17:33:36

      好的!

  2. Bob : 2011年09月09日18:14:13  -48楼

    加一个,供参考:
    http://bbs.chinaunix.net/viewthread.php?tid=3592317&from=favorites

    • clairelume : 2011年09月09日18:41:48

      呵呵,学习了,有空参考一下!~ 😛

  3. 肠粉的做法 : 2012年06月15日16:12:24  -47楼

    🙂 实现这个回收站功能 真的很使用

  4. 锻铜雕塑 : 2012年09月12日15:37:15  -46楼

    学习了 技术贴啊 前段时间正好想到过这个问题 茅塞顿开 谢谢分享

  5. 明夜心 : 2015年05月07日17:04:26  -45楼

    不错,用到了