Skills: File Operation Skills

Last updated: 2026-08-31

Files are the carriers of code — a Skill that can't operate files is like a designer without hands.


1. File Operation Toolbox

(1) Read Operations

Operation Tool Use Case
Read single file Read Review specific files
Search filenames Glob Determine file scope
Search file contents Grep Locate code patterns

(2) Write Operations

Operation Tool Risk Use Case
Precise edit Edit 🟡 Bug fix, small adjustments
Create/overwrite Write 🔴 New files, complete rewrites
Batch modify Edit + loop 🟡 Rename, batch replacement

(3) Execution Operations

Operation Tool Use Case
Format code Bash(prettier) Code style unification
Run scripts Bash(python) Data processing, batch operations
Git operations Bash(git) Version management

2. File Operation Modes

(1) Read-Only Analysis Mode

No files modified, only analysis output:

YAML
---
name: code-metrics
description: "Code metrics analysis"
tools:
  - Read
  - Grep
  - Glob
---
MARKDOWN
## Execution Flow
1. Glob to determine project file scope
2. Grep to count code patterns (classes, functions, TODOs)
3. Read to deeply analyze key files
4. Output metrics report, no files modified

(2) Safe Modification Mode

Backup before modification, verify after:

MARKDOWN
## Safe Modification Rules
1. Read current file contents
2. Analyze what needs to be modified
3. Edit precisely, only change target lines
4. Bash to run tests for verification
5. If tests fail, explain the issue and suggest rollback

(3) Batch Operation Mode

MARKDOWN
## Batch Rename Flow
1. Glob to get target file list
2. List all planned changes
3. Edit one by one to execute changes
4. Bash to run full test suite for verification

3. Search Strategies

(1) Search-First Principle

TEXT 📖 Display only
Wrong approach: Read all files → Check one by one
Right approach: Search to locate → Precisely read

(2) Common Search Patterns

Search Target Grep Pattern Example
Function definitions `def \w+ function \w+`
TODO comments TODO|FIXME|HACK Find tech debt
Security risks eval(|exec(|subprocess Find dangerous calls
Type definitions class \w+|interface \w+ Find type structures
Test files test_|_test|\.spec\. Find test files

(3) Search + Read Coordination

100%
graph TD
    A[Glob: Determine file list] --> B[Grep: Search key patterns]
    B --> C{Found target?}
    C -->|Yes| D[Read: Deep read]
    C -->|No| E[Expand search scope]
    D --> F{Need modification?}
    F -->|Yes| G[Edit: Precise modification]
    F -->|No| H[Output analysis result]

4. File Operation Practice

▶ Example: Project Migration Skill

Alice needs to migrate a project from JavaScript to TypeScript:

YAML
---
name: js-to-ts-migration
description: "Migrate JS files to TS"
tools:
  - Read
  - Write
  - Glob
  - Grep
  - Bash
---
MARKDOWN
## Migration Flow
1. Glob to find all .js files
2. For each file:
   a. Read contents
   b. Analyze dependencies and exports
   c. Write corresponding .ts file
   d. Add type annotations
3. Bash to run tsc --noEmit for verification
4. Output migration report

Bob said: "The biggest fear with file operations is accidental deletion or modification — search to locate, precise editing, post-verification — none of these three steps can be skipped."


❓ FAQ

Q How to choose between Edit and Write?
A Prefer Edit for modifying existing files (line-precise), use Write for creating new files. Never use Write to overwrite a small part of a large file.
Q How to avoid missing files in batch operations?
A Use Glob to list all target files first, confirm scope before operating. Record progress at each step for recovery after interruption.
Q How to handle large files?
A Use Grep to locate target line numbers, then use Read with line number ranges to read relevant fragments, avoiding loading the entire file and wasting context.

📖 Summary


📝 Exercises

  1. Basic (⭐): Create a read-only analysis Skill that counts TODO/FIXME occurrences and distribution in a project.
  2. Intermediate (⭐⭐): Create a safe modification Skill that auto-adds type hints to Python functions, then runs tests for verification.
  3. Advanced (⭐⭐⭐): Create a batch migration Skill that converts CommonJS require to ES Module import, handling edge cases like circular dependencies.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏