file-operations

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

File Operations

文件操作

Analyze files and retrieve metadata using Claude's native tools without modifying files.
使用Claude的原生工具分析文件并检索元数据,无需修改文件。

When to Use

适用场景

  • "analyze [file]"
  • "get file info for [file]"
  • "how many lines in [file]"
  • "compare [file1] and [file2]"
  • "file statistics"
  • "分析[文件]"
  • "获取[文件]的信息"
  • "[文件]有多少行"
  • "比较[文件1]和[文件2]"
  • "文件统计"

Core Operations

核心操作

File Size & Metadata

文件大小与元数据

bash
stat -f "%z bytes, modified %Sm" [file_path]  # Single file
ls -lh [directory]                             # Multiple files
du -h [file_path]                              # Human-readable size
bash
stat -f "%z bytes, modified %Sm" [file_path]  # 单个文件
ls -lh [directory]                             # 多个文件
du -h [file_path]                              # 易读格式的文件大小

Line Counts

行数统计

bash
wc -l [file_path]                              # Single file
wc -l [file1] [file2]                          # Multiple files
find [dir] -name "*.py" | xargs wc -l          # Directory total
bash
wc -l [file_path]                              # 单个文件
wc -l [file1] [file2]                          # 多个文件
find [dir] -name "*.py" | xargs wc -l          # 目录总行数

Content Analysis

内容分析

Use Read to analyze structure, then count functions/classes/imports.
使用Read工具分析文件结构,然后统计函数/类/导入语句的数量。

Pattern Search

模式搜索

Grep(pattern="^def ", output_mode="count", path="src/")        # Count functions
Grep(pattern="TODO|FIXME", output_mode="content", -n=true)    # Find TODOs
Grep(pattern="^import ", output_mode="count")                 # Count imports
Grep(pattern="^def ", output_mode="count", path="src/")        # 统计函数数量
Grep(pattern="TODO|FIXME", output_mode="content", -n=true)    # 查找TODO标记
Grep(pattern="^import ", output_mode="count")                 # 统计导入语句数量

Find Files

文件查找

Glob(pattern="**/*.py")
Glob(pattern="**/*.py")

Workflow Examples

工作流示例

Comprehensive File Analysis

全面文件分析

  1. Get size/mod time:
    stat -f "%z bytes, modified %Sm" file.py
  2. Count lines:
    wc -l file.py
  3. Read file:
    Read(file_path="file.py")
  4. Count functions:
    Grep(pattern="^def ", output_mode="count")
  5. Count classes:
    Grep(pattern="^class ", output_mode="count")
  1. 获取大小/修改时间:
    stat -f "%z bytes, modified %Sm" file.py
  2. 统计行数:
    wc -l file.py
  3. 读取文件:
    Read(file_path="file.py")
  4. 统计函数数量:
    Grep(pattern="^def ", output_mode="count")
  5. 统计类数量:
    Grep(pattern="^class ", output_mode="count")

Compare File Sizes

比较文件大小

  1. Find files:
    Glob(pattern="src/**/*.py")
  2. Get sizes:
    ls -lh src/**/*.py
  3. Total size:
    du -sh src/*.py
  1. 查找文件:
    Glob(pattern="src/**/*.py")
  2. 获取文件大小:
    ls -lh src/**/*.py
  3. 总大小:
    du -sh src/*.py

Code Quality Metrics

代码质量指标

  1. Total lines:
    find . -name "*.py" | xargs wc -l
  2. Test files:
    find . -name "test_*.py" | wc -l
  3. TODOs:
    Grep(pattern="TODO|FIXME|HACK", output_mode="count")
  1. 总行数:
    find . -name "*.py" | xargs wc -l
  2. 测试文件数量:
    find . -name "test_*.py" | wc -l
  3. TODO标记数量:
    Grep(pattern="TODO|FIXME|HACK", output_mode="count")

Find Largest Files

查找最大文件

bash
find . -type f -not -path "./node_modules/*" -exec du -h {} + | sort -rh | head -20
bash
find . -type f -not -path "./node_modules/*" -exec du -h {} + | sort -rh | head -20

Best Practices

最佳实践

  • Non-destructive: Use Read/stat/wc, never modify
  • Efficient: Read small files fully, use Grep for large files
  • Context-aware: Compare to project averages, suggest optimizations
  • 非破坏性操作:使用Read/stat/wc等工具,绝不修改文件
  • 高效性:小文件完全读取,大文件使用Grep工具
  • 上下文感知:与项目平均值对比,提出优化建议

Integration

集成

Works with:
  • code-auditor: Comprehensive analysis
  • code-transfer: After identifying large files
  • codebase-documenter: Understanding file purposes
可与以下工具集成:
  • code-auditor:全面分析
  • code-transfer:识别大文件后使用
  • codebase-documenter:了解文件用途