remove-small-dirs 628 B

12345678910111213
  1. #!/usr/bin/env bash
  2. [[ $# -eq 0 ]] && echo "You must provide a directory" && exit 1
  3. dir="$1"
  4. [[ $# -eq 2 ]] && SIZE="$2" || SIZE=1000 # set the default min dir size
  5. [[ ! -d "$dir" ]] && echo "Directory does not exist" && exit 1
  6. # Print dirs to be removed
  7. find "$dir" -mindepth 1 -type d -exec du -ks {} + | awk -v size="$SIZE" '$1 <= size' | cut -f 2-
  8. read -r -p "Is this OK? [y/N]" _response
  9. _response_l="${_response,,}"
  10. [[ ! "$_response_l" =~ ^(yes|y) ]] && echo "Exiting, no changes were made" && exit 0
  11. find "$dir" -mindepth 1 -type d -exec du -ks {} + | awk -v size="$SIZE" '$1 <= size' | cut -f 2- | xargs -d'\n' rm -rf