clean-downloads-dir.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. DATE="$(date +%Y/%m/%d)" # yyyy/mm/dd
  4. DATE_IGNORE_PATTERN="20[0-9][0-9]"
  5. DIR="${HOME}/Downloads" # no slash at the end!
  6. DATE_DIR="${DIR}/${DATE}/"
  7. BIG_FILES="big_files"
  8. BIG_FILES_DIR="${DIR}/${BIG_FILES}/"
  9. move_files(){
  10. mapfile -t files < <(find "$DIR" -maxdepth 1 -mindepth 1 -not -name "$DATE_IGNORE_PATTERN" -not -name "$BIG_FILES" -not -name "*.part")
  11. if [[ ${#files[@]} -eq 0 ]]; then
  12. echo "No files to organize"
  13. exit 2
  14. fi
  15. mkdir -pv "$DATE_DIR"
  16. mv -v "${files[@]}" "$DATE_DIR"
  17. }
  18. del_empty_files(){
  19. find "$DIR" -empty -delete
  20. }
  21. move_big_files(){
  22. mapfile -t files < <(find "$DIR" -type f -size +1G -not -name "$BIG_FILES" -not -name "*.part")
  23. if [[ ${#files[@]} -eq 0 ]]; then
  24. echo "No files to organize"
  25. exit 2
  26. fi
  27. mkdir -pv "$BIG_FILES_DIR"
  28. mv -v "${files[@]}" "$BIG_FILES_DIR"
  29. }
  30. read -rp "Are your sure you want to move everything from ${DIR}/* to ${DATE_DIR} (Y[es]/N): " ans
  31. if [[ 'yes' =~ ^${ans,,} ]]; then
  32. move_files
  33. move_big_files
  34. del_empty_files
  35. fi