find.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. BOLD='\e[1;31m' # Bold Red
  4. REV='\e[1;32m' # Bold Green
  5. display_help(){
  6. echo -e "${BOLD}Usage: [starting-location] [name-of-searching-file] ${OFF}"
  7. echo -e "${BOLD}Usage:${REV} $(basename $0) -e ~/Torrents/transmission_finished ${OFF}"
  8. echo -e "${REV}The following switches are recognized. ${OFF} "
  9. echo -e "${REV}-e ${BOLD} --Searches and deletes empty files and directories in current of specified path ${OFF}"
  10. echo -e "${REV}-p ${BOLD} --Searches for *.part files (unfinished transmission torrent) and deletes them (use with causion) ${OFF}"
  11. echo -e "${REV}-s +-=[size (in megabytes)] ${BOLD} --Searches for files larger than specified size${OFF}"
  12. echo -e "${REV}-t +-=[days] ${BOLD} --Searches for files modified from now to specified days back${OFF}"
  13. echo -e "${REV}-h${BOLD} --Displays this help message. No further functions are performed.${OFF}"
  14. exit 2
  15. }
  16. [[ $# -lt 1 ]] && display_help
  17. if [ -d "$1" ]; then
  18. PWD="$1"
  19. shift
  20. else
  21. PWD=$(pwd -P)
  22. fi
  23. fd="find $PWD -mount"
  24. # In case you wanted to check what variables were passed
  25. # echo "flags = $*"
  26. while getopts ':n:s:t:eph' opt; do
  27. case $opt in
  28. n) $fd -iname "*${OPTARG}*" | grep --color -i "$OPTARG" ;;
  29. e) list=$($fd -empty) ;;
  30. #e) $fd -empty -exec trash-put "{}" \; -print ;;
  31. p) list=$($fd -type f -name "*.part$") ;;
  32. # p) $fd -type f -name "*.part$" -exec trash-put "{}" \; -print ;;
  33. s) $fd -type f -size "${OPTARG}M" -exec du -h "{}" \; | sort -n ;;
  34. # t) $fd -type f -mtime "-${OPTARG}" -exec sort -c '%y %n' "{}" \; | sort -n ;;
  35. t) $fd -type f -mtime "${OPTARG}" -exec ls -gort --full-time "{}" \; | sed 's/\..........//' | cut -d' ' -f4,5,7- ;;
  36. h|\?|:|*) display_help ;;
  37. esac
  38. done
  39. case "$1" in
  40. -e | -p)
  41. echo "$list"
  42. read -rp 'Do you want to delete above files? (y,n): ' ans
  43. [[ $ans == "Y" || $ans == 'y' ]] && trash-put "$list"
  44. # (trash-put -v $list | grep -v "Volume of file: \|Trash-dir: ") # something dont work woth this grep
  45. ;;
  46. *)exit 2 ;;
  47. esac