← 返回
未分类

文件整理

Organize files in a directory by file extension. Sorts files into category folders (Documents, Images, Videos, Music, Archives, Code, Others) and provides a summary report. Use when the user says "clean up my desktop", "organize files", "sort downloads folder", or "tidy up".
Organize files in a directory by file extension. Sorts files into category folders (Documents, Images, Videos, Music, Archives, Code, Others) and provides a summary report. Use when the user says "clean up my desktop", "organize files", "sort downloads folder", or "tidy up".
user_d2ac1320
未分类 community v1.0.1 2 版本 100000 Key: 无需
★ 0
Stars
📥 108
下载
💾 0
安装
2
版本
#latest

概述

file-organizer

Organize files in a specified folder by classifying them into category directories based on file extension.

Usage

/file-organizer [target-dir]

Default target: user's Desktop.

Category Rules (configurable)

CategoryExtensions
------------------------
Documents.pdf .doc .docx .xls .xlsx .ppt .pptx .txt .md .csv .rtf .odt .epub .pages .numbers .key
Images.jpg .jpeg .png .gif .bmp .svg .webp .ico .tiff .tif .psd .ai
Videos.mp4 .avi .mkv .mov .wmv .flv .webm .m4v .mpeg .mpg .3gp
Music.mp3 .wav .flac .aac .ogg .wma .m4a .opus .alac
Archives.zip .rar .7z .tar .gz .bz2 .xz .zst .iso .dmg .cab
Code.js .ts .py .java .cpp .c .h .hpp .cs .go .rs .rb .php .swift .kt .scala .html .css .json .xml .yaml .yml .toml .ini .cfg .sh .bat .ps1 .sql .graphql .proto .dockerfile
Executables.exe .msi .dll .app .deb .rpm .apk .dmg
Fonts.ttf .otf .woff .woff2 .eot
Otherseverything else

Implementation

Use the Bash tool to run the organizer. The script pattern:

TARGET_DIR="${1:-$HOME/Desktop}"  # default: desktop

# dry-run: pass "--dry-run" as second arg
DRY_RUN=false
if [ "$2" = "--dry-run" ]; then DRY_RUN=true; fi

# Define categories
declare -A CAT_MAP
CAT_MAP[pdf]="Documents"; CAT_MAP[docx]="Documents"; CAT_MAP[doc]="Documents"
CAT_MAP[xlsx]="Documents"; CAT_MAP[pptx]="Documents"; CAT_MAP[txt]="Documents"
CAT_MAP[md]="Documents"; CAT_MAP[csv]="Documents"

CAT_MAP[jpg]="Images"; CAT_MAP[jpeg]="Images"; CAT_MAP[png]="Images"
CAT_MAP[gif]="Images"; CAT_MAP[bmp]="Images"; CAT_MAP[svg]="Images"
CAT_MAP[webp]="Images"; CAT_MAP[ico]="Images"

CAT_MAP[mp4]="Videos"; CAT_MAP[avi]="Videos"; CAT_MAP[mkv]="Videos"
CAT_MAP[mov]="Videos"; CAT_MAP[wmv]="Videos"; CAT_MAP[flv]="Videos"

CAT_MAP[mp3]="Music"; CAT_MAP[wav]="Music"; CAT_MAP[flac]="Music"
CAT_MAP[aac]="Music"; CAT_MAP[ogg]="Music"

CAT_MAP[zip]="Archives"; CAT_MAP[rar]="Archives"; CAT_MAP[7z]="Archives"
CAT_MAP[tar]="Archives"; CAT_MAP[gz]="Archives"; CAT_MAP[bz2]="Archives"

CAT_MAP[js]="Code"; CAT_MAP[ts]="Code"; CAT_MAP[py]="Code"
CAT_MAP[java]="Code"; CAT_MAP[html]="Code"; CAT_MAP[css]="Code"
CAT_MAP[json]="Code"; CAT_MAP[xml]="Code"; CAT_MAP[yaml]="Code"
CAT_MAP[yml]="Code"; CAT_MAP[sh]="Code"; CAT_MAP[bat]="Code"
CAT_MAP[go]="Code"; CAT_MAP[rs]="Code"; CAT_MAP[cpp]="Code"
CAT_MAP[c]="Code"; CAT_MAP[php]="Code"; CAT_MAP[rb]="Code"

CAT_MAP[exe]="Executables"; CAT_MAP[msi]="Executables"
CAT_MAP[dll]="Executables"; CAT_MAP[app]="Executables"

CAT_MAP[ttf]="Fonts"; CAT_MAP[otf]="Fonts"; CAT_MAP[woff]="Fonts"

# ---- script ----
echo "=== File Organizer ==="
echo "Target: $TARGET_DIR"
$DRY_RUN && echo "Mode: DRY RUN (no files will be moved)"

# Find all files (not dirs) directly in target
declare -A COUNTS
TOTAL=0

while IFS= read -r -d '' file; do
  rel="${file#$TARGET_DIR/}"
  base="$(basename "$rel")"
  ext="${base##*.}"

  # skip if no extension or hidden file
  [ "$ext" = "$base" ] && continue
  [[ "$base" == .* ]] && continue

  ext_lc=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
  cat="${CAT_MAP[$ext_lc]:-Others}"

  # count
  ((COUNTS[$cat]++))
  ((TOTAL++))

  if $DRY_RUN; then
    echo "  [$cat] $rel"
  fi
done < <(find "$TARGET_DIR" -maxdepth 1 -type f -print0)

echo ""
echo "=== Summary ==="
echo "Total files found: $TOTAL"
echo ""

if $DRY_RUN; then
  echo "Run without --dry-run to move files."
  exit 0
fi

# Create dirs and move
for cat in "${!COUNTS[@]}"; do
  mkdir -p "$TARGET_DIR/$cat"
done

while IFS= read -r -d '' file; do
  rel="${file#$TARGET_DIR/}"
  base="$(basename "$rel")"
  ext="${base##*.}"
  [ "$ext" = "$base" ] && continue
  [[ "$base" == .* ]] && continue

  ext_lc=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
  cat="${CAT_MAP[$ext_lc]:-Others}"

  # handle name collision
  dest="$TARGET_DIR/$cat/$base"
  if [ -f "$dest" ]; then
    name_no_ext="${base%.*}"
    dest="$TARGET_DIR/$cat/${name_no_ext}_$(date +%Y%m%d_%H%M%S).$ext"
  fi

  mv "$file" "$dest"
done < <(find "$TARGET_DIR" -maxdepth 1 -type f -print0)

echo "=== Organization Report ==="
printf "%-20s %s\n" "Category" "Files"
printf "%-20s %s\n" "--------" "-----"
for cat in Documents Images Videos Music Archives Code Executables Fonts Others; do
  [ -n "${COUNTS[$cat]}" ] && printf "%-20s %d\n" "$cat" "${COUNTS[$cat]}"
done
echo ""
echo "Done! Files organized in: $TARGET_DIR"

Dry-run first

Always offer to do a dry-run before actually moving files. Run with --dry-run:

bash <<'SCRIPT'
TARGET_DIR="$HOME/Desktop"
# ... (same script with DRY_RUN=true)
SCRIPT

Undo (restore)

If the user wants to undo, the action is not reversible via script (files have been moved). You can restore by re-organizing within each category folder, but the simplest path is:

  • Tell the user to open the category folders and drag files back
  • Or, if they want the flat list back, run:
  • find "$TARGET_DIR" -mindepth 2 -type f -exec mv {} "$TARGET_DIR" \;
    

Tips

  • Always dry-run first before moving
  • Show the report clearly after organization
  • The working directory must have execute permissions for find, mkdir, mv
  • Works on Windows via Git Bash / WSL (the paths use forward slashes)

版本历史

共 2 个版本

  • v1.0.1 Initial release 当前
    2026-05-17 17:00 安全 安全
  • v1.0.0 Initial release
    2026-05-17 16:51 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 677 📥 325,910
ai-agent

self-improving agent

pskoett
捕获经验教训、错误及修正内容,以实现持续改进。适用于以下场景:(1)命令或操作意外失败;(2)用户纠正Claude(如“不,那不对……”“实际上……”);(3)用户请求的功能不存在;(4)外部API或工具出现故障;(5)Claude发现自身
★ 4,086 📥 814,839
ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,385 📥 321,018