clean_trash.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env bash
  2. # 设置定量 | Quantities
  3. ## 初始化变量 | Initialize variables
  4. VERBOSE=0
  5. VERSION="1.1.0"
  6. ## 当前语言 | Current language
  7. CURRENT_LANG=0 # 0: en-US, 1: zh-Hans-CN
  8. ## 定义回收站目录 | Define trash directory
  9. TRASH_DIR="${TRASH_DIR:-$HOME/.local/share/Trash}"
  10. FILES_DIR="${FILES_DIR:-$TRASH_DIR/files}"
  11. INFO_DIR="${INFO_DIR:-$TRASH_DIR/info}"
  12. EXPUNGED_DIR="${EXPUNGED_DIR:-$TRASH_DIR/expunged}"
  13. # 本地化 | Localization
  14. recho() {
  15. if [ "$CURRENT_LANG" == "1" ]; then
  16. ## zh-Hans-CN
  17. echo "$1";
  18. else
  19. ## en-US
  20. echo "$2";
  21. fi
  22. }
  23. # 显示帮助信息 | Show help message
  24. show_help() {
  25. recho "回收站清理工具" "Trash cleaner tool"
  26. recho "用法:$0 [-h] [-v] [-V] <保留天数>" "Usage: $0 [-h] [-v] [-V] <days>"
  27. recho "选项:" "Options:"
  28. recho " -h 显示帮助信息" " -h Show help"
  29. recho " -v 详细输出模式" " -v Verbose mode"
  30. recho " -V 显示版本信息" " -V Show version"
  31. recho "示例:" "Examples:"
  32. recho " $0 30 # 清理 30 天前的回收站文件" " $0 30 # Clean trash files older than 30 days"
  33. recho " $0 -v 7 # 详细模式清理 7 天前文件" " $0 -v 7 # Verbose mode to clean files older than 7 days"
  34. }
  35. # 显示版本信息 | Show version
  36. show_version() {
  37. recho "回收站清理工具 v$VERSION" "Trash cleaner tool v$VERSION"
  38. }
  39. # 语言检测 | Language detection
  40. if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1; fi
  41. # 解析选项 | Parse options
  42. # 处理长选项 | Handle long options
  43. TEMP=$(getopt -o 'hvV' --long 'help,verbose,version' -n 'clean_trash.sh' -- "$@")
  44. if [ $? != 0 ]; then
  45. recho "错误:无效选项" "Error: Invalid option" >&2
  46. exit 1
  47. fi
  48. eval set -- "$TEMP"
  49. while true; do
  50. case "$1" in
  51. -h|--help) show_help ; exit 0 ;;
  52. -v|--verbose) VERBOSE=1 ; shift ;;
  53. -V|--version) show_version ; exit 0 ;;
  54. --) shift ; break ;;
  55. *) recho "错误:无效选项" "Error: Invalid option" >&2; exit 1 ;;
  56. esac
  57. done
  58. shift $((OPTIND-1))
  59. # 检查是否提供了天数参数 | Check if days parameter was provided
  60. if [ $# -ne 1 ]; then
  61. recho "错误:需要提供保留天数参数" "Error: Need to provide days parameter" >&2
  62. show_help
  63. exit 1
  64. fi
  65. # 获取天数参数 | Get days parameter
  66. DAYS=$1
  67. # 检查天数是否为正整数 | Check if days is a positive integer
  68. if ! [[ "$DAYS" =~ ^[0-9]+$ ]] || [ "$DAYS" -le 0 ]; then
  69. recho "错误:天数必须是正整数" "Error: Days must be a positive integer" >&2
  70. exit 1
  71. fi
  72. # 检查回收站目录结构 | Check trash directory structure
  73. if [ ! -d "$FILES_DIR" ] || [ ! -d "$INFO_DIR" ]; then
  74. recho "错误:回收站目录结构不正确" "Error: Trash directory structure is incorrect" >&2
  75. recho "请确认目录存在:$FILES_DIR 和 $INFO_DIR" "Please confirm directories exist: $FILES_DIR and $INFO_DIR" >&2
  76. exit 1
  77. fi
  78. # 获取当前时间戳 | Get current timestamp
  79. CURRENT_TIME=$(date +%s)
  80. # 计算截止时间戳 | Calculate cutoff timestamp
  81. CUTOFF_TIME=$((CURRENT_TIME - DAYS * 24 * 60 * 60))
  82. # 初始化计数器 | Initialize counters
  83. DELETED_COUNT=0
  84. ERROR_COUNT=0
  85. # 确保 expunged 目录存在 | Ensure expunged directory exists
  86. mkdir -p "$EXPUNGED_DIR"
  87. # 使用 find 避免内存中的文件名扩展 | Use find to avoid filename expansion in memory
  88. # 使用 process substitution 和 while 循环处理每个文件 | Use process substitution and while loop to process each file
  89. while IFS= read -r -d '' INFO_FILE; do
  90. # 检查文件是否存在 | Check if file exists
  91. if [ ! -f "$INFO_FILE" ]; then
  92. continue
  93. fi
  94. # 获取对应的文件名 | Get corresponding filename
  95. BASENAME=$(basename "$INFO_FILE" .trashinfo)
  96. TRASH_FILE="$FILES_DIR/$BASENAME"
  97. # 检查对应的文件是否存在 | Check if corresponding file exists
  98. if [ ! -e "$TRASH_FILE" ]; then
  99. if [ $VERBOSE -eq 1 ]; then
  100. recho "警告:找不到对应文件 $TRASH_FILE" "Warning: Cannot find corresponding file $TRASH_FILE" >&2
  101. fi
  102. continue
  103. fi
  104. # 从 .trashinfo 文件中提取 DeletionDate | Extract DeletionDate from .trashinfo file
  105. # 使用内置参数替换代替 grep/cut 管道 | Use built-in parameter substitution instead of grep/cut pipeline
  106. while IFS='=' read -r KEY VALUE; do
  107. if [ "$KEY" = "DeletionDate" ]; then
  108. DELETION_DATE="$VALUE"
  109. break
  110. fi
  111. done < "$INFO_FILE"
  112. # 检查是否成功提取日期 | Check if date was extracted successfully
  113. if [ -z "$DELETION_DATE" ]; then
  114. if [ $VERBOSE -eq 1 ]; then
  115. recho "警告:无法从 $INFO_FILE 提取删除日期" "Warning: Cannot extract deletion date from $INFO_FILE" >&2
  116. fi
  117. continue
  118. fi
  119. # 将删除日期转换为时间戳 | Convert deletion date to timestamp
  120. # 注意:日期格式为 2025-08-13T19:47:35
  121. DELETION_TIMESTAMP=$(date -d "$DELETION_DATE" +%s 2>/dev/null)
  122. # 检查日期转换是否成功 | Check if date conversion was successful
  123. if [ $? -ne 0 ] || [ -z "$DELETION_TIMESTAMP" ]; then
  124. if [ $VERBOSE -eq 1 ]; then
  125. recho "警告:无法解析删除日期 $DELETION_DATE" "Warning: Cannot parse deletion date $DELETION_DATE" >&2
  126. fi
  127. continue
  128. fi
  129. # 检查文件是否超过保留天数 | Check if file exceeds retention days
  130. if [ "$DELETION_TIMESTAMP" -lt "$CUTOFF_TIME" ]; then
  131. # 创建唯一的 expunged 文件名 | Create unique expunged filename
  132. EXPUNGED_FILE="$EXPUNGED_DIR/$BASENAME"
  133. # 如果文件已存在,则添加时间戳以确保唯一性 | If file exists, add timestamp to ensure uniqueness
  134. if [ -e "$EXPUNGED_FILE" ]; then
  135. EXPUNGED_FILE="$EXPUNGED_DIR/${BASENAME}_$(date +%s)"
  136. fi
  137. # 将文件移动到 expunged 目录 | Move file to expunged directory
  138. if [ $VERBOSE -eq 1 ]; then
  139. recho "移动文件到 expunged: $TRASH_FILE (删除于 $DELETION_DATE)" "Moving file to expunged: $TRASH_FILE (deleted at $DELETION_DATE)"
  140. fi
  141. if mv "$TRASH_FILE" "$EXPUNGED_FILE"; then
  142. # 删除元数据文件 | Delete metadata file
  143. rm -f "$INFO_FILE"
  144. DELETED_COUNT=$((DELETED_COUNT + 1))
  145. else
  146. ERROR_COUNT=$((ERROR_COUNT + 1))
  147. recho "错误:无法移动文件到 expunged 目录" "Error: Cannot move file to expunged directory" >&2
  148. fi
  149. fi
  150. # 重置变量以节省内存 | Reset variables to save memory
  151. unset DELETION_DATE DELETION_TIMESTAMP
  152. done < <(find "$INFO_DIR" -name '*.trashinfo' -print0 2>/dev/null)
  153. # 物理删除 expunged 目录中的所有文件 | Physically delete all files in expunged directory
  154. if [ -d "$EXPUNGED_DIR" ]; then
  155. if [ $VERBOSE -eq 1 ]; then
  156. # 使用 stat 而不是 find | wc -l 来计算文件数 | Use stat instead of find | wc -l to count files
  157. EXPUNGED_COUNT=$(find "$EXPUNGED_DIR" -type f -printf '.' 2>/dev/null | wc -c)
  158. if [ "$EXPUNGED_COUNT" -gt 0 ]; then
  159. recho "物理删除 expunged 目录中的 $EXPUNGED_COUNT 个文件" "Physically deleting $EXPUNGED_COUNT files in expunged directory"
  160. fi
  161. fi
  162. # 物理删除 expunged 目录中的所有内容 | Physically delete all contents in expunged directory
  163. rm -rf "$EXPUNGED_DIR"/* 2>/dev/null
  164. fi
  165. # 输出结果 | Output results
  166. recho "清理完成:删除了 $DELETED_COUNT 个文件,$ERROR_COUNT 个错误" "Cleanup completed: $DELETED_COUNT files deleted, $ERROR_COUNT errors"