安全的delete命令

   系统的rm命令常常导致一些不愉快的事情。默认情况下rm 不会在删除文件前提是用户是否真的想这么做,删除后也不能再从系统中恢复。这意味着用户不得不为自己的意识糊涂付出惨痛的代价。

   设计一个相对“安全”的delete命令来替代rm。一下是代码,请各位验收

##建立回收站机制
##将需要删除的文件移动到~/.trash中
if [ ! -d ~/.trash ]
then
        mkdir ~/.trash
fi

if [ $# -eq 0 ]
then
        #提示delete的用法
        echo "Usage:delete file1 [file2 file3 ...]"
else
        echo "You are about to delete these files:"
        echo $@
        #要求用户确认是否删除这些文件。回答N或n放弃删除,其他字符表示确认
        read -p "Are you sure to do that? [Y/n]:" reply
        if [ "$reply" != "n" ] && [ "$reply" != "N" ]
        then 
                for file in $@
                do
                        #判断文件活目录是否存在
                        if [ -f "$file" ] || [ -d "$file" ]
                        then
                                mv -b "$file" ~/.trash/
      else
                                echo "$file: NO such file or directory"
                        fi
                done
        #如果用户回答N或n
        else
                echo "NO file removed"
        fi
fi


# 这个delete不完美, 他不能处理文件名中存在空格的情况

# touch "zhang san"

# delete zhang\ san
You are about to delete these files:
zhang san
Are you sure to do that? [Y/n]:y
zhang: NO such file or directory
san: NO such file or directory


你可能感兴趣的:(安全,delete)