免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4837 | 回复: 10

【原创】命令行回收站 [复制链接]

论坛徽章:
0
发表于 2011-09-06 22:08 |显示全部楼层
本帖最后由 tianlijian 于 2011-09-07 12:25 编辑

命令行回收站


[387 0 ~]$ rm -h
rm version 1.0
Usage: rm [-r] [-f]  [file]...
del file and move it the recycle

[388 0 ~]$ recycle -h
recycle version 1.0
Usage: recycle [-l] [-n] [-r]
       recycle -d -empty
       recycle -d [-f] [-t <time>] [-s <size>]
manipulate the recycle

Options:
  h - Help
  l - list the deleted file with long format
  n - sort the deleted file by name
  r - list the deleted whth reverse order

  ______________________________________
  d - delete file mode
  e - empty the recycle
  t <time> -  delete the file `time' days ago
  s <size> - delete file which size greater the `size' megabyte
  1. #! /bin/bash


  2. RECYCLE_DIR=~/.recycle
  3. VERSION=1.0

  4. function echo.log {

  5.         local line_no=${BASH_LINENO[0]}
  6.         local file_name=$0
  7.        
  8.         echo "${file_name}:${line_no}:$@"
  9. }

  10. echo.ok() {
  11.         if [[ $1 == "-n" ]]; then
  12.                 shift
  13.                 echo -n -e "\\033[1;32m$@\e[0m"
  14.         else
  15.                 echo -e "\\033[1;32m$@\e[0m"
  16.         fi
  17. }
  18. echo.err() {
  19.         if [[ $1 == "-n" ]]; then
  20.                 shift
  21.                 echo -n -e "\\033[1;31m$@\e[0m"
  22.         else
  23.                 echo -e "\\033[1;31m$@\e[0m"
  24.         fi
  25. }


  26. function yesOrNo {
  27.     local answer=""
  28.     local ret=0

  29.     local message="$@, (Y)es/(N)o? [n] "
  30.     : ${REDIRECT:=/dev/tty}

  31.     while true ; do
  32.                 read -p "${message}" answer < $REDIRECT > $REDIRECT 2>&1
  33.                 case "$answer" in
  34.                         [yY]) ret=0; break ;;
  35.                         [nN]|"")         ret=1; break ;;
  36.                         *)         echo; continue
  37.                 esac
  38.     done


  39.     return $ret
  40. }


  41. mvToRecycle() {

  42.         ### file OR dir not exist
  43.         if [[ ! -a "$1" ]]; then
  44.                 echo.err rm: cannot remove \`$1\': No such file or directory
  45.                 return 1
  46.         fi

  47.         if [[ -d "$1" ]]; then
  48.                 t="directory"
  49.                 st="d"
  50.         else
  51.                 t="file"
  52.                 st="f"
  53.         fi

  54.         ### dir
  55.         if [[ -d "$1" && "$option" != *r* ]]; then
  56.                 echo.err rm: cannot remove \`$1\'\: Is a directory
  57.                 return 1
  58.         fi

  59.         ### prompt
  60.         if [[ $option != *f* ]]; then
  61.                 ### if type no, return
  62.                 yesOrNo "remove $t \`$1'" \
  63.                 || return 0
  64.         fi

  65.         ### basename
  66.         basename=${1%/}   ### 如果末尾有/,则去掉
  67.         basename=${basename##*/} ### 去掉前面的路径部分

  68.         ### remove dir
  69.         mv  -- "$1" "${RECYCLE_DIR}/${DATE}_${st}_${basename}"  \
  70.                         && echo.ok removed $t \`$1\'

  71. }






  72. print_help()
  73. {
  74. cat <<-EOF
  75.         ${0##*/} version $VERSION
  76.         Usage: ${0##*/} [-r] [-f]  [file]...
  77.         del file and move it to ~/.recycle

  78.         EOF
  79.     # Handle unclosed quote for syntax highlighting '
  80. }




  81. ### read option
  82. option=""
  83. while (( $# >= 1 )); do
  84.         case "$1" in
  85.                 -h|--help)        print_help; exit 0;;
  86.                 --)                shift; break;; #遇到`--',则option结束
  87.                 -*)                option="${option}$1"; shift;;
  88.                 *)                        break;;
  89.         esac
  90. done



  91. DATE=$(date +%Y%m%d_%H%M%S)
  92. while (( $# >= 1 )); do
  93.         #echo.log "$1"
  94.         mvToRecycle "$1"
  95.         shift
  96. done
复制代码

bin.zip

3.64 KB, 下载次数: 73

评分

参与人数 1可用积分 +2 收起 理由
rdcwayx + 2 支持原创

查看全部评分

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
发表于 2011-09-06 23:26 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
巳蛇
日期:2013-10-28 15:55:33
发表于 2011-09-07 08:06 |显示全部楼层
把文件移到当前文件系统指定的目录, 谓之回收站。。。

论坛徽章:
0
发表于 2011-09-07 09:13 |显示全部楼层
吴用

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:55:28
发表于 2011-09-07 10:26 |显示全部楼层
没看明白,recycle是lz原创的?那为啥只有help没有源代码?我们还看个啥。。。

论坛徽章:
0
发表于 2011-09-07 11:27 |显示全部楼层
你用的什么系统呢? 我的fedora 木有recycle

论坛徽章:
0
发表于 2011-09-07 11:31 |显示全部楼层
这贴。。。真牛。。。无语。。。

论坛徽章:
0
发表于 2011-09-07 14:50 |显示全部楼层
{:2_181:}看不怎么懂呢

论坛徽章:
1
巳蛇
日期:2013-10-28 15:55:33
发表于 2011-09-07 16:57 |显示全部楼层
建议解析mount/du命令或/proc/mounts文件的结果, 把文件移到对应文件系统下的recycle目录, 不然删除大文件怎么办? 慢慢等?

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2011-09-07 18:43 |显示全部楼层
bjgirl在8年前有个类似作品:安全删除和恢复文件的脚本
http://bbs.chinaunix.net/thread-228037-1-1.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP