File & Directory Remove Script


/ Published in: Bash
Save to your folder(s)

Modified a little bit to make it easier to change the location of the recycle bin.


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. #
  3. # This script is to be called by an alias of the rm command
  4. # e.g. using the entry...
  5. # alias rm='sh /home/user1/my_scripts/rm_replacement.sh'
  6. # in your $HOME/.profile or $HOME/.bashrc file
  7. #
  8. # It will move rm files to /home/user1/.RecycleBin
  9. # You need to create the directory /home/user1/.RecycleBin if
  10. # it doesn't already exist
  11. #
  12. # If a file/directory with the same name already exists in the
  13. # RecycleBin, the file/directory already in the RecycleBin
  14. # will be renamed taging "_X" on to the end where
  15. # X is an incremental number and then the newly rm'd
  16. # file/directory will be copied into the RecycleBin
  17. #
  18. # Consequently the latest version of the file will be its normall
  19. # name, without any extension.
  20.  
  21. RM_OPT=""
  22. FILES=""
  23. VERBOSE="n"
  24. FORCE="n"
  25. VOLUME="/home/user1"
  26.  
  27. while [ "$1" ]; do
  28. case "$1" in
  29. "-v") VERBOSE="y" ;;
  30. "-f") FORCE="y" ;;
  31. "--help" | "-h")
  32. echo "You are running a replacement for rm called by an alias"
  33. echo "Usage rm [options] file1 file2 ..."
  34. echo
  35. echo "Moves the files to /volume1/RecycleBin. Files can be either files or"
  36. echo "directories."
  37. echo
  38. echo "Options:"
  39. echo " -v verbose mode"
  40. echo " -h, --help this help message"
  41. echo " --help-rm help message of /bin/rm"
  42. echo " -f force processing with the normal rm command (/bin/rm)"
  43. echo " All other options are ignored when moving files. When removing"
  44. echo " permanently, these options are passed to /bin/rm"
  45. echo
  46. exit 0
  47. ;;
  48. "--help-rm")
  49. /bin/rm --help
  50. exit 0
  51. ;;
  52. -*) RM_OPT=$RM_OPT" "$1 ;;
  53. *)
  54. if [ "$VERBOSE" = "y" ]; then
  55. echo $1
  56. fi
  57. if [ "$FORCE" = "y" ]; then
  58. /bin/rm -f $RM_OPT $1
  59. else
  60. if [ -e "$1" ]; then
  61. if [ -e "$VOLUME/.RecycleBin/$(basename $1)" ]; then
  62. version=2
  63. while [ -e "$VOLUME/.RecycleBin/$(basename $1)_$version" ]; do
  64. let version=$version+1
  65. done
  66. mv "$VOLUME/.RecycleBin/$(basename $1)" "$VOLUME/.RecycleBin/$(basename $1)_$version"
  67. fi
  68. mv "$1" "$VOLUME/.RecycleBin"
  69. fi
  70. fi
  71. ;;
  72. esac
  73. shift
  74. done

URL: http://forum.synology.com/wiki/index.php/How_to_create_a_Recycle_Bin_(or_Trash_can)_for_the_CLI_rm_command

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.