Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Settings
  5. #
  6.  
  7. # Prevents the script to continue running when one command fails
  8. set -e
  9. # set -x
  10.  
  11. # Enabling extended syntax (useful for excluding files in rm commands)
  12. shopt -s extglob
  13.  
  14. #
  15. # Functions
  16. #
  17.  
  18. # Prints help message
  19. function usage {
  20. echo "This program allows you to delete files according to a pattern, in the given directories, by excluding the directories defined by the user."
  21. echo "Arguments:"
  22. echo -e " -h\t\t\tPrints this help message"
  23. echo -e " -p <pattern>\t\tThe pattern that will be used to delete the files"
  24. echo -e " -d directory1[,...]\tA comma separated list of directories in which to delete the files based on the pattern"
  25. echo -e " -e directory1[,...]\tA comma separated list of the directories to exclude"
  26. echo -e " -o n{smhdw}\t\tOnly the files older than n seconds, n minutes, etc will be deleted."
  27. echo
  28. echo "The arguments -p and -d are mandatory."
  29. }
  30.  
  31. # Deletes the files in the given directory and its subdirectories
  32. # $1: pattern
  33. # $2: directory in which files must be deleted based on the pattern
  34. # $3: excluded directories
  35. function rm_in_subdirs {
  36. local pattern=$1
  37. local dir=$2
  38.  
  39. cd $dir
  40.  
  41. exclusion=""
  42. if [ ! -z "$EXCLUDED" ]; then
  43. local excluded=( `echo "$EXCLUDED"` )
  44. echo "Excluded dirs: ${excluded[@]}"
  45. exclusion="-path ./${excluded[0]}"
  46. for excluded_dir in "${excluded[@]:1}"
  47. do
  48. exclusion="$exclusion -o -path ./$excluded_dir "
  49. done
  50. fi
  51.  
  52. if [ -z "$MTIME_ARG" ]; then
  53. mtime=""
  54. else
  55. mtime="-mtime +$MTIME_ARG"
  56. fi
  57.  
  58. if [ -z "$exclusion" ]; then
  59. find . -type f -name "file*.txt" $mtime | xargs rm
  60. else
  61. find . \! \( \( $exclusion \) -prune \) -type f -name "file*.txt" $mtime | xargs rm
  62. fi
  63. }
  64.  
  65. #
  66. # Handling command line option
  67. #
  68. while getopts d:e:p:o:h option
  69. do
  70. IFS=','
  71. case "$option" in
  72. d)
  73. read -ra DIRECTORIES <<< "$OPTARG"
  74. ;;
  75. e)
  76. read -ra EXCLUDED_DIR <<< "$OPTARG"
  77. ;;
  78. p)
  79. export PATTERN=$OPTARG
  80. ;;
  81. o)
  82. # output date format: to comply with touch -t option
  83. export MTIME_ARG=$OPTARG
  84. ;;
  85. h)
  86. usage
  87. exit 0
  88. ;;
  89. esac
  90. unset IFS
  91. done
  92.  
  93. echo "Directories : ${DIRECTORIES[@]}"
  94. echo "Excuded ones: ${EXCLUDED_DIR[@]}"
  95. echo "Pattern : $PATTERN"
  96. echo
  97.  
  98. # If one of the mandatory params is not present: print help, leave the program.
  99. if [ -z $PATTERN ] || [ -z $DIRECTORIES ]; then
  100. usage
  101. exit 1
  102. fi
  103.  
  104. #
  105. # Deleting the files.
  106. #
  107. for directory in "${DIRECTORIES[@]}"
  108. do
  109. export EXCLUDED=${EXCLUDED_DIR[@]}
  110. rm_in_subdirs $PATTERN $directory
  111. done
  112.  
  113. echo "done"
  114.  
  115. unset EXCLUDED
  116. unset PATTERN
  117. unset MTIME_ARG
Add Comment
Please, Sign In to add comment