Big Bug Ban

兴趣 践行 创新

安全的rm

 

一个不小心rm掉文件了吧?

后悔莫及了吧!

把这段代码加入你的home目录的.bashrc或者.zshrc就可以了

  1. ### by 3haku.net
  2. function saferm() {
  3.     ops_array=($*)
  4.     if [[ z $1 ]] ;then
  5.         echo ‘Missing Args’
  6.         return
  7.     fi
  8.     J=0
  9.     offset=0
  10.     # for zsh
  11.     if [[ z ${ops_array[0]} ]] ; then
  12.         offset=1
  13.     fi
  14.     while [[ $J lt $# ]] ; do
  15.           p_posi=$(($J + $offset))
  16.           dst_name=${ops_array[$p_posi]}
  17.           if [[ `echo ${dst_name} | cut -c 1` == ‘-‘ ]] ; then
  18.                 continue
  19.           fi
  20.         # garbage collect
  21.         now=$(date +%s)
  22.         for s in $(ls indicatorstyle=none $HOME/.trash/) ;do
  23.             dir_name=${s//_/-}
  24.             dir_time=$(date +%d $dir_name)
  25.             # if big than one month then delete
  26.             if [[ 0 eq dir_time || $(($now  $dir_time)) gt 2592000 ]] ;then
  27.                 echo “Trash “ $dir_name ” has Gone “
  28.                    /bin/rm $HOME/.trash/$dir_name rf
  29.             fi
  30.         done
  31.     
  32.         # add new folder
  33.         prefix=$(date +%Y_%m_%d)
  34.         hour=$(date +%H)
  35.          mkdir p $HOME/.trash/$prefix/$hour
  36.         echo “Trashing “ $dst_name
  37.         mv ./$dst_name $HOME/.trash/$prefix/$hour
  38.           J=$(($J+1))
  39.     done
  40. }
  41. alias rm=saferm

工作原理:

在你的home目录会创建一个.trash文件夹

里面会按照删除时间 年-月-日/小时/ 进行归档已删除的文件

然后会删除一个月以前的文件夹

就是这样!

Written by princehaku

2月 1st, 2013 at 12:47 上午

Posted in linux

Tagged with

with 3 comments

3 Responses to '安全的rm'

Subscribe to comments with RSS or TrackBack to '安全的rm'.

  1. 这方面了解太少了,看来要加强。

    藏章博客

    6 2月 13 at 10:13 下午

  2. Big bug BAN 呢? 突然 改名字了

    dodio

    26 2月 13 at 9:48 上午

  3. 恩 换了

    princehaku

    28 2月 13 at 12:20 上午

Leave a Reply