1
0

clean_trash.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bash
  2. # 设置定量 | Quantities
  3. ## 初始化变量 | Initialize variables
  4. VERBOSE=0
  5. VERSION="1.0.1"
  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. # 遍历 info 目录中的所有 .trashinfo 文件 | Iterate through all .trashinfo files in info directory
  88. for INFO_FILE in "$INFO_DIR"/*.trashinfo; do
  89. # 检查文件是否存在 | Check if file exists
  90. if [ ! -f "$INFO_FILE" ]; then
  91. continue
  92. fi
  93. # 获取对应的文件名 | Get corresponding filename
  94. BASENAME=$(basename "$INFO_FILE" .trashinfo)
  95. TRASH_FILE="$FILES_DIR/$BASENAME"
  96. # 检查对应的文件是否存在 | Check if corresponding file exists
  97. if [ ! -e "$TRASH_FILE" ]; then
  98. if [ $VERBOSE -eq 1 ]; then
  99. recho "警告:找不到对应文件 $TRASH_FILE" "Warning: Cannot find corresponding file $TRASH_FILE" >&2
  100. fi
  101. continue
  102. fi
  103. # 从 .trashinfo 文件中提取 DeletionDate | Extract DeletionDate from .trashinfo file
  104. DELETION_DATE=$(grep -E "^DeletionDate=" "$INFO_FILE" | cut -d'=' -f2)
  105. # 检查是否成功提取日期 | Check if date was extracted successfully
  106. if [ -z "$DELETION_DATE" ]; then
  107. if [ $VERBOSE -eq 1 ]; then
  108. recho "警告:无法从 $INFO_FILE 提取删除日期" "Warning: Cannot extract deletion date from $INFO_FILE" >&2
  109. fi
  110. continue
  111. fi
  112. # 将删除日期转换为时间戳 | Convert deletion date to timestamp
  113. # 注意:日期格式为 2025-08-13T19:47:35
  114. DELETION_TIMESTAMP=$(date -d "$DELETION_DATE" +%s 2>/dev/null)
  115. # 检查日期转换是否成功 | Check if date conversion was successful
  116. if [ $? -ne 0 ] || [ -z "$DELETION_TIMESTAMP" ]; then
  117. if [ $VERBOSE -eq 1 ]; then
  118. recho "警告:无法解析删除日期 $DELETION_DATE" "Warning: Cannot parse deletion date $DELETION_DATE" >&2
  119. fi
  120. continue
  121. fi
  122. # 检查文件是否超过保留天数 | Check if file exceeds retention days
  123. if [ "$DELETION_TIMESTAMP" -lt "$CUTOFF_TIME" ]; then
  124. # 创建唯一的 expunged 文件名 | Create unique expunged filename
  125. EXPUNGED_FILE="$EXPUNGED_DIR/$BASENAME"
  126. # 如果文件已存在,则添加时间戳以确保唯一性 | If file exists, add timestamp to ensure uniqueness
  127. if [ -e "$EXPUNGED_FILE" ]; then
  128. EXPUNGED_FILE="$EXPUNGED_DIR/${BASENAME}_$(date +%s)"
  129. fi
  130. # 将文件移动到 expunged 目录 | Move file to expunged directory
  131. if [ $VERBOSE -eq 1 ]; then
  132. recho "移动文件到 expunged: $TRASH_FILE (删除于 $DELETION_DATE)" "Moving file to expunged: $TRASH_FILE (deleted at $DELETION_DATE)"
  133. fi
  134. if mv "$TRASH_FILE" "$EXPUNGED_FILE"; then
  135. # 删除元数据文件 | Delete metadata file
  136. rm -f "$INFO_FILE"
  137. DELETED_COUNT=$((DELETED_COUNT + 1))
  138. else
  139. ERROR_COUNT=$((ERROR_COUNT + 1))
  140. recho "错误:无法移动文件到 expunged 目录" "Error: Cannot move file to expunged directory" >&2
  141. fi
  142. fi
  143. done
  144. # 物理删除 expunged 目录中的所有文件 | Physically delete all files in expunged directory
  145. if [ -d "$EXPUNGED_DIR" ]; then
  146. if [ $VERBOSE -eq 1 ]; then
  147. EXPUNGED_COUNT=$(find "$EXPUNGED_DIR" -mindepth 1 2>/dev/null | wc -l)
  148. if [ "$EXPUNGED_COUNT" -gt 0 ]; then
  149. recho "物理删除 expunged 目录中的 $EXPUNGED_COUNT 个文件" "Physically deleting $EXPUNGED_COUNT files in expunged directory"
  150. fi
  151. fi
  152. # 物理删除 expunged 目录中的所有内容 | Physically delete all contents in expunged directory
  153. rm -rf "$EXPUNGED_DIR"/* 2>/dev/null
  154. fi
  155. # 输出结果 | Output results
  156. recho "清理完成:删除了 $DELETED_COUNT 个文件,$ERROR_COUNT 个错误" "Cleanup completed: $DELETED_COUNT files deleted, $ERROR_COUNT errors"