专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

del/restore脚本

百度收藏 QQ搜藏

一般的del/undel都是把“删除”的文件移到某一个目录下。
这样就有可能会出现一些跨文件系统移动大文件的情况,而且还可能有文件属性改变的情况。

我这个脚本用的是就地“刨坑埋”的办法。只是将“删除”的文件改个名,并在统一的目录记录下来,当删除记录文件时才真正把文件删掉。
日志目录是$HOME/.trash/
下面是脚本,
del删除文件,做个叫restore的链接,实现恢复。
emptytrash清空回收站。

del代码:
#!/bin/bash
wd="$PWD"
while [ $# != "0" ]
do
 if [ "${1#/}" != "$1" ]
 then
  file="$1"
 else
  file="$wd/$1"
 fi
 bname="`basename "$file"`"
 cd "`dirname "$file"`"
 if [ "$PWD" == "$HOME/.trash" ]
 then
  if ! [ -f "$file" ]
  then
   echo "$file" no exit
   exit
  fi
  exec 3<&0
  exec <"$file"
  read deldir
  read delfile
  read ofile
  exec <&3-
  if [ xx"$deldir" == xx ] || [ "${delfile#".del_$ofile"}" == "${delfile}" ]
  then
   echo file \"$file\" Error
   exit
  fi
  if [ `basename $0` == del ]
  then
   /bin/rm -fr "$deldir/$delfile"
   /bin/rm "$bname"
  elif [ `basename $0` == restore ]
  then
   mv -i "$deldir/$delfile" "$deldir/$ofile"
   if ! [ -a "$deldir/$delfile" ]
   then
    /bin/rm "$bname"
   fi
  fi
 elif [ `basename $0` == del ]
 then
  tl=`date +%F-%H-%M-%S`
  mv "$bname" ".del_${bname}_$tl" && echo -e "$PWD\n.del_${bname}_$tl\n$bname" > "$HOME/.trash/_${bname}_${tl}.ash"
 fi
 shift
done

emptytrash代码:
#!/bin/bash
echo -n 'Empty Trash?(Y/N) '
read ans
if [ "$ans" == "Y" ]
then
 cd $HOME/.trash
 del *
fi

上一篇:一个演示汉诺塔的脚本 下一篇:监视文件系统的使用量

power by soyo123 2007-2008