• Linux 安全rm


    先将shell脚本放在某个全局路径下,如/usr/local/bin

    #!/bin/sh
    # safe rm
    # Don't remove the file, just move them to a temporary directory.
    # Files are grouped by remove time.
    # e.g.
    #   # pwd => /home/work/
    #   > rm -r -f aa
    #   'aa' will move to ~/.TrashHistory/20141018/aa@120111@_home_work_aa
    RM_BACKUP_PATH=~/.Trashhistory
    if [ ! -d "$RM_BACKUP_PATH" ]; then
       mkdir "$RM_BACKUP_PATH"
    fi
    function safe_rm() {
       # skip cmd option, e.g. '-rf' in 'rm -rf a b' or '-r/-f' in 'rm -r -f a b'
       if [ $# -lt 1 ]; then
           echo 'usage: rm -f [-dPRrvW] file ...'
           exit 1
       fi
       first_char=${1:0:1}
       until [ ! "$first_char" = "-" ]
       do
           shift
           first_char=${1:0:1}
       done
       # check param
       if [ $# -lt 1 ]; then
           echo 'usage: rm -f [-dPRrvW] file ...'
           exit 1
       fi
       today=`date +"%Y%m%d"`
       mvpath=${RM_BACKUP_PATH}/$today
       # support for multi version
       timestamp=`date +"%H%M%S"`
       # create dir if path non-exist
       if [ ! -d $mvpath ]; then
           mkdir $mvpath
       fi
       until [ $# -eq 0 ]
       do
           # fetch absolute path of the file
           fpath=$1
           fchar=`echo "${fpath:0:1}"`
           if [ "$fchar" = "/" ]; then
               dist_path="_${fpath}"
           else
               abs_fpath=`pwd`/$fpath
               dist_path="${fpath}@${timestamp}@${abs_fpath}"
           fi
           # substitue '/' to '_'
           final_dist_path=${dist_path////_}
           # mv to temp trash
           mv $fpath $mvpath/$final_dist_path
           # next file
           shift
       done
    }
    

      然后alias

    alias rm=". /usr/local/bin/safe_rm.sh && safe_rm "
    

      并source

  • 相关阅读:
    结构体和指针
    冒泡排序、指针、函数
    指针学习笔记
    C++学习笔记
    背包问题
    零钱兑换2【DP】
    零钱兑换1【DP】
    执行hadoop jar命令时报Not a valid JAR的原因分析和解决方案
    斐波那契数列
    具有n个结点的二叉树有多少种形态
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/5160628.html
Copyright © 2020-2023  润新知