Guest User

Untitled

a guest
Jun 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # author: markus tornow
  4. # date: Fri Jul 9 23:44:50 CEST 2010
  5. # name: move_old
  6. # path: ~/bin
  7. #### #### #### ####
  8. # usage: $0 *<file-ending-pattern_0> *<file-ending-pattern_n>
  9. # Don't forget the * in front of the file-endings you add :-=
  10.  
  11. clear; date; echo
  12.  
  13. # VARIABLES
  14. ###################
  15.  
  16. EXCLUDE="$0"
  17. OLD_DIR=old-$(date +%y-%m-%d)
  18. OLD="old"
  19. INDEX=0
  20.  
  21. # Check if all parameters are given
  22. # minimum is one in the form of *<pattern>
  23. ###########################################
  24.  
  25. if [ "$#" -eq 0 ]
  26. then
  27. echo "
  28. Usage is: "$0" *<pattern_0> *<pattern_n>
  29. With pattern being what you want to move to old
  30. "
  31. fi
  32. ####################################################
  33. # Check for a backup directory, called old_timestamp
  34. # Avoid an error by omitting its creation in case it
  35. # exists
  36. # VARs
  37. #####################################################
  38.  
  39. if [[ -d $OLD/$OLD_DIR ]]
  40. then
  41. mv "$OLD/$OLD_DIR" "$OLD/$OLD_DIR-$INDEX"
  42. ((INDEX+=1))
  43. mkdir "$OLD_DIR"
  44. else
  45. mkdir "$OLD_DIR"
  46. fi
  47.  
  48. if ! [[ -d $OLD ]]
  49. then
  50. mkdir "$OLD"
  51. fi
  52.  
  53. ###########################################
  54. # Create a FUNKTION,
  55. # to double-check with a dry run (with echo)
  56. # if all is cool
  57. ############################################
  58.  
  59. function ask_for {
  60. echo -n "
  61. do you want to move on (yes or no): "
  62. read answer
  63. if [[ $answer = yes ]] || [[ $answer = y ]]
  64. then
  65. echo "
  66. ok
  67. "
  68. else
  69. echo "
  70. check whats wrong
  71. "
  72. exit 1
  73. fi
  74. }
  75.  
  76. ####################
  77. # dry run
  78. ####################
  79.  
  80. for pattern in "$@"
  81. do
  82. if ! [[ $pattern =~ $EXCLUDE ]] || ! [[ $pattern = OLD ]]
  83. then
  84. echo "$pattern"
  85. fi
  86. done
  87.  
  88. ask_for
  89. # in case user answer no
  90. # nothing will be moved
  91.  
  92. #####################
  93. # The final command
  94. #####################
  95.  
  96. for pattern in "$@"
  97. do
  98. if ! [[ $pattern =~ $EXCLUDE ]]
  99. then
  100. mv $pattern $OLD_DIR
  101. fi
  102. done
  103.  
  104. mv $OLD_DIR $OLD
  105.  
  106. exit 0
Add Comment
Please, Sign In to add comment