码迷,mamicode.com
首页 > 系统相关 > 详细

shell-自定义rm

时间:2018-10-04 17:27:51      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:获取   span   size   磁盘空间   ==   目录   color   date   done   

<<COMMENT
功能介绍:
1.删除操作执行前先获取文件或目录的大小,然后对比系统中的磁盘空间
2.如果有多余空间,则创建对应的隐藏目录,并备份
3.如果没有足够的空间,可以提醒用户是否放弃备份,如果放弃,则直接删除
COMMENT
filename=$1
d=`date +%Y%m%d%H%M`
f_size=`du -sk $1|awk {print $1}`
disk_size=`LANG=en; df -k |grep -iv filesystem |awk {print $4} |sort -n |tail -n 1`
big_filesystem=`LANG=en; df -k |grep -iv filesystem |sort -n -k4 |tail -n 1 |awk {print $NF}`
if [ $f_size -lt $disk_size ]; then
  read -p "Are U sure delete the file or directory $1 y|n:" c
  if [ $c=="y" ] || [ $c=="Y" ]; then
    mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/ && /bin/rm -rf $1
  elif [ $c=="n" ] || [ $c=="N" ]; then
    exit 0
  fi
else
  echo "The disk size is not enough to backup the files $1"
  read -p "Do you want to delete "$1"? y|n:" c
  if [ $c=="y" ] || [ $c=="Y" ]; then
    echo "It will delete "$1" after 5 seconds whitout backup"
    for i in `seq 1 5`; do echo -ne "."; sleep 1;done
  echo 
    /bin/rm -rf $1
  elif [ $c=="n" ] || [ $c=="N" ]; then
    echo "It will not delete $1."
    exit 0
  else
    echo "Please input ‘y‘ or ‘n‘"
  fi
fi

 

shell-自定义rm

标签:获取   span   size   磁盘空间   ==   目录   color   date   done   

原文地址:https://www.cnblogs.com/watchslowly/p/9742504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!