搞懂 .claude/ 資料夾:Claude Code 的控制中樞,CLAUDE.md、指令、技能與權限完整解析

ChainNewsAbmedia

大多數 Claude Code 使用者知道

.claude/

資料夾的存在,但從未真正打開過它。AI 工程師 Akshay 近日整理了一份完整指南,解析這個資料夾的每個檔案的功能,以及如何設定才能讓 Claude 完全按照你的方式工作。

兩個資料夾,不是一個

首先要釐清一個常見誤解:

.claude/

資料夾有兩個,不是一個。

專案層級(你的專案/.claude/):存放團隊共用設定,提交到 Git,讓所有人獲得一致的規則與指令

全域層級(~/.claude/):個人偏好與跨專案設定,只影響自己的機器

CLAUDE.md:最重要的一個檔案

每次啟動 Claude Code 工作階段,Claude 第一件事就是讀取

CLAUDE.md

,並將其載入系統提示詞(system prompt),在整個對話中持續遵守其中的指示。

應該寫入的內容:

建置、測試、lint 指令(如 npm run test)

重要的架構決策

不明顯的注意事項(例如「TypeScript strict mode 開啟,未使用的變數會報錯」)

命名規範、錯誤處理風格

不應該寫入的內容:適合放在 linter 設定的規則、完整文件、冗長的理論說明。

Akshay 建議將 CLAUDE.md 控制在 200 行以內——超過這個長度,Claude 的指令遵守率實際上會下降,因為消耗太多 context。

rules/ 資料夾:模組化指令,適合團隊擴展

當 CLAUDE.md 越來越臃腫,

.claude/rules/

是解法。每個 Markdown 檔案代表一個關注點,例如 code-style.md、 testing.md、 api-conventions.md,Claude 會自動讀取所有檔案。

更強大的是「路徑範圍規則」:在規則檔案加入 YAML 前置資料,就能讓規則只在 Claude 處理特定路徑的檔案時才載入,避免不相關的規則佔用 context。

commands/ 資料夾:自訂斜線指令

放在

.claude/commands/

的每個 Markdown 檔案都會成為一個斜線指令。 review.md 對應 /project:review, fix-issue.md 對應 /project:fix-issue。

最實用的功能是在指令檔案中使用

!

語法執行 shell 指令並嵌入輸出——例如自動抓取 git diff 注入提示詞,讓 Claude 真正「看到」你的程式碼變更。放在 ~/.claude/commands/ 的個人指令則跨所有專案都可使用。

skills/ 與 agents/:主動觸發 vs. 指定子代理

Skills 和 agents 的核心差異在於觸發方式:

Skills:Claude 根據對話內容自動判斷是否呼叫,無需手動輸入指令。每個 skill 有自己的目錄與 SKILL.md,並可附帶支援檔案

Agents:定義專業子代理人格,擁有獨立的系統提示詞、工具權限與模型設定。複雜任務時 Claude 會 spawn 一個隔離的 context window 讓代理執行,避免主工作階段被大量 token 塞滿

Agents 的

tools

欄位能限制子代理的行為範圍——安全審計代理只需要讀取權限,不應有寫入能力。 model 欄位則讓你為聚焦型任務選用較輕量的模型,節省成本。

settings.json:權限白名單與黑名單

.claude/settings.json

控制 Claude 被允許或禁止執行的操作:

allow 清單:直接執行,不需確認(例如 npm run *、git *)

deny 清單:完全封鎖(例如 rm -rf *、讀取 .env)

不在清單內的操作:Claude 會詢問是否繼續

個人設定可放在

.claude/settings.local.json

,自動被 gitignore,不會提交到倉庫。

從哪裡開始?

Akshay 建議的實用起步順序:先執行

/init

生成初始 CLAUDE.md,加入 settings.json 設定基本權限,再建立一兩個最常用的自訂指令——其餘的隨著使用習慣逐步添加。

核心洞察是:

.claude/

資料夾是你告訴 Claude「你是誰、專案是什麼、應該遵守什麼規則」的協定。設定得越清楚,花在糾正 Claude 的時間就越少。

這篇文章 搞懂 .claude/ 資料夾:Claude Code 的控制中樞,CLAUDE.md、指令、技能與權限完整解析 最早出現於 鏈新聞 ABMedia。

Disclaimer: The information on this page may come from third parties and does not represent the views or opinions of Gate. The content displayed on this page is for reference only and does not constitute any financial, investment, or legal advice. Gate does not guarantee the accuracy or completeness of the information and shall not be liable for any losses arising from the use of this information. Virtual asset investments carry high risks and are subject to significant price volatility. You may lose all of your invested principal. Please fully understand the relevant risks and make prudent decisions based on your own financial situation and risk tolerance. For details, please refer to Disclaimer.
Opmerking
0/400
Geen opmerkingen