分享

定制一个相对安全的linux命令

 torony 2015-12-26

        在linux下, 用rm是比较危险的, 删除了的文件不会放到所谓的回收站中, 有很多因rm而“一失足成一天恨”的例子, 下面, 我们考虑来定制一个delete命令, 并模拟做一个回收站, 这样, 即使delete文件/目录, 也会放在自建的回收站中, 相对比较安全。

       

       用shell script来实现, delete文本文件的内容为:

  1. #! /bin/bash  
  2. #  customize a relatively safer command, namely delete, to substitute rm  
  3.   
  4.   
  5. # create a dustbin if necessary  
  6. if [ ! -d ~/.RecycleBin ]  
  7. then   
  8.         mkdir ~/.RecycleBin  
  9. fi  
  10.   
  11. # necessary, the shell script file(delete) and the file/directory to be deleted(e.g. a.txt or folder) are not necessarily in the same directory  
  12. current_dir=`pwd`  
  13.   
  14. if [ $# -eq 0 ]  
  15. then   
  16.         echo "Usage: delete file1 [file2 file3 ...]"   # guide to use delete  
  17. else   
  18.         echo "My dear, you are about to delete --- :"  
  19.         echo $@  
  20.           
  21.         read -p "Are you sure to delete it(them)? [y/n]:" user_answer  
  22.         if [ "$user_answer" == "y" ] || [ "$user_answer" == "Y" ]  
  23.         then    
  24.                 for file in $@  
  25.                 do   
  26.                         if [ ${file:(-1)} == "/" ]   # last character  
  27.                         then  
  28.                             file=${file%?}  #  cut the last /  
  29.                         fi  
  30.                           
  31.                         if [ -f "$current_dir/$file" ] || [ -d "$current_dir/$file" ]  
  32.                         then  
  33.                                 # calculate the current date  
  34.                                 current_date=`date`  
  35.                                 current_date=${current_date// /_}     # replace blank with _  
  36.                                   
  37.                                 dest_file="${file}__DelTime:$current_date"  
  38.                                       
  39.                                 mv "$current_dir/$file"  "$current_dir/$dest_file"  
  40.                                 mv "$current_dir/$dest_file" ~/.RecycleBin     # move the file/directory to dustbin, not "~/.RecycleBin", but ~/.RecycleBin  
  41.                         else  
  42.                                 echo "$file: No such file or directory, my dear!"  
  43.                         fi  
  44.                 done  
  45.         else  
  46.                 echo "No file or directory removed"  
  47.         fi  
  48. fi  

       注意, 要对文件加可执行权限。


       我们查阅一下PATH的值, 并把delete这个文件放到对应的目录下(如果没有, 可以自建一个目录), 然后执行一系列的操作, 结果如下:

  1. [taoge@localhost learn_shell]$ echo $PATH  
  2. /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/taoge/bin:/home/taoge/bin  
  3. [taoge@localhost learn_shell]$ ls /home/taoge/bin/delete   
  4. /home/taoge/bin/delete  
  5. [taoge@localhost learn_shell]$ ls ~/.RecycleBin/  
  6. [taoge@localhost learn_shell]$ ls  
  7. [taoge@localhost learn_shell]$ touch a.txt; mkdir folder  
  8. [taoge@localhost learn_shell]$ delete a.txt folder  
  9. My dear, you are about to delete --- :  
  10. a.txt folder  
  11. Are you sure to delete it(them)? [y/n]:y  
  12. [taoge@localhost learn_shell]$ touch a.txt; mkdir folder  
  13. [taoge@localhost learn_shell]$ delete a.txt folder/  
  14. My dear, you are about to delete --- :  
  15. a.txt folder/  
  16. Are you sure to delete it(them)? [y/n]:y  
  17. [taoge@localhost learn_shell]$   
  18. [taoge@localhost learn_shell]$   
  19. [taoge@localhost learn_shell]$   
  20. [taoge@localhost learn_shell]$ ls -l ~/.RecycleBin/  
  21. total 8  
  22. -rw-rw-r-- 1 taoge taoge    0 May  9 22:15 a.txt__DelTime:Sat_May__9_22:16:13_PDT_2015  
  23. -rw-rw-r-- 1 taoge taoge    0 May  9 22:16 a.txt__DelTime:Sat_May__9_22:16:38_PDT_2015  
  24. drwxrwxr-x 2 taoge taoge 4096 May  9 22:15 folder__DelTime:Sat_May__9_22:16:13_PDT_2015  
  25. drwxrwxr-x 2 taoge taoge 4096 May  9 22:16 folder__DelTime:Sat_May__9_22:16:38_PDT_2015  
  26. [taoge@localhost learn_shell]$   

       这个~/.RecycleBin就是我们自建的回收站。 另外要注意: /home/taoge/bin的bin是binary,  是存放二进制文件的目录, 当然在本例中, 我把脚本文件放进去了, 也无妨。 .RecycleBin的bin是dustbin.


       OK,  本文先介绍到这里, 上述程序基本能满足要求。 当然, 要做到非常稳健, 还是有一些地方需要改进的(比如, 如果文件或者目录中有空格, 则上述代码有错)。






    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多