Skip to content

forgeekscn/clean-mac-cli

Repository files navigation

Clean Mac CLI

一款强大的 macOS 磁盘清理命令行工具,帮助您快速释放磁盘空间。

功能特性

基础功能

  • 智能扫描: 自动扫描缓存、日志、临时文件
  • 安全删除: 内置黑名单/白名单机制,保护系统关键文件
  • 废纸篓支持: 优先移动到废纸篓,支持恢复

进阶功能

  • 开发工具清理: 支持 Xcode、Docker、npm、yarn、pip、go 等开发工具缓存
  • 大文件扫描: 识别大于 100MB 的文件
  • 重复文件检测: 查找并标记重复文件

高级功能

  • 清理历史: 记录所有清理操作
  • 报告导出: 支持 JSON、CSV、TXT 格式导出
  • 配置管理: 支持自定义清理规则
  • 定时任务: 支持 cron 定时自动清理

安装

使用 Homebrew(推荐)

# 方法 1: 从官方 Homebrew Core 安装(如果已合并)
brew install clean-mac

# 方法 2: 从作者的 Tap 安装
brew tap forgeekscn/homebrew-clean-mac
brew install clean-mac

# 方法 3: 从源码安装
brew install clean-mac --build-from-source

使用 npm

# 全局安装
npm install -g clean-mac-cli

# 使用
clean-mac help

下载预编译二进制

访问 Releases 下载适合你系统的版本:

# macOS Intel
curl -L https://github.com/forgeekscn/clean-mac-cli/releases/latest/download/clean-mac-darwin-amd64.zip -o clean-mac.zip
unzip clean-mac.zip
sudo mv clean-mac /usr/local/bin/

# macOS Apple Silicon
curl -L https://github.com/forgeekscn/clean-mac-cli/releases/latest/download/clean-mac-darwin-arm64.zip -o clean-mac.zip
unzip clean-mac.zip
sudo mv clean-mac /usr/local/bin/

从源码编译

# 克隆仓库
git clone https://github.com/forgeekscn/clean-mac-cli.git
cd clean-mac-cli

# 编译
make build

# 安装
make install

使用方法

基本命令

# 查看帮助
clean-mac help

# 扫描可清理文件
clean-mac scan

# 清理文件(交互式)
clean-mac clean

# 强制清理(跳过确认)
clean-mac clean --force

扫描选项

# 扫描包括开发工具缓存
clean-mac scan --dev

# 仅扫描基础文件
clean-mac scan

定时任务

# 检查 cron 是否可用
clean-mac schedule

# 添加定时任务(每天凌晨 2 点清理)
clean-mac schedule add "0 2 * * *"

# 移除定时任务
clean-mac schedule remove

查看历史

# 查看清理历史
clean-mac history

导出报告

# 导出为 JSON(默认)
clean-mac export

# 导出为 CSV
clean-mac export --format=csv

# 导出为 TXT
clean-mac export --format=txt

配置文件

配置文件位于 ~/.clean-mac/config.json

{
  "autoCleanDevTools": false,
  "skipPrompts": false,
  "excludedPaths": [
    "/Users/username/Important/Cache"
  ],
  "minLargeFileSize": 104857600,
  "reportFormat": "json",
  "schedule": "0 2 * * *"
}

安全机制

黑名单(永远不删除)

  • 系统关键路径:/System, /Library, /usr, /bin
  • 用户关键目录:Desktop, Documents, Downloads
  • 安全文件:.ssh, .gnupg, Keychains

白名单(安全可删除)

  • 缓存文件:*.cache, *.tmp, *.temp
  • 日志文件:*.log
  • 临时目录:*/Caches/*, */tmp/*, */Logs/*
  • 开发工具缓存:DerivedData, .npm, .yarn

清理类型

类型 说明 示例路径
Cache 应用缓存 ~/Library/Caches/*
Logs 应用日志 ~/Library/Logs/*
Temporary Files 临时文件 /tmp/*
Dev Tools Cache 开发工具缓存 ~/Library/Developer/Xcode/DerivedData
Large Files 大文件(>100MB) ~/Downloads/*
Duplicate Files 重复文件 相同大小的文件

开发工具支持

  • Xcode: DerivedData, Archives
  • Docker: 容器缓存, 镜像层
  • Node.js: npm cache, yarn cache
  • Python: pip cache, pycache
  • Go: go-build cache
  • Ruby: gem cache
  • Java: Maven/Gradle cache

系统要求

  • macOS 10.15 Catalina 或更高版本
  • Go 1.18+ (仅编译时需要)
  • cron (用于定时任务,可选)

常见问题

Q: 清理会删除我的重要文件吗?

A: 不会。工具内置了黑名单机制,默认只清理缓存、日志和临时文件。清理前会显示所有待删除文件供您确认。

Q: 如何恢复误删的文件?

A: 如果启用了废纸篓模式(默认),可以在废纸篓中找到并恢复。如果直接删除,请使用 Time Machine 或其他备份工具恢复。

Q: 定时任务需要什么条件?

A: 需要系统支持 cron。macOS 默认支持,但可能需要手动启动。使用 clean-mac schedule 检查可用性。

Q: 可以自定义清理规则吗?

A: 可以。编辑 ~/.clean-mac/config.json 配置文件,添加排除路径或修改清理规则。

贡献

欢迎提交 Issue 和 Pull Request!

许可证

MIT License

作者

Your Name - @forgeekscn

致谢

  • 感谢所有贡献者
  • 感谢 Go 社区的支持

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors