Skip to content

File Operations

Learn how Claude Code CLI reads, creates, and modifies files directly on your filesystem.

What You'll Learn

  • How Claude CLI reads files from disk
  • Creating new files in your terminal session
  • Editing existing files directly
  • Working with multiple files
  • Understanding the permission system

How the CLI Accesses Files

Unlike the Claude web interface where you copy/paste code, Claude Code CLI has direct filesystem access in your working directory. It uses specialized tools internally:

Tool Purpose
Read View file contents
Write Create new files
Edit Modify existing files
Glob Find files by pattern
Grep Search file contents

You don't call these directly - just describe what you want.

Reading Files

Single File

> Show me the contents of package.json

Claude reads and displays the file.

> What does the main function in src/index.js do?

Claude reads the file and explains it.

Multiple Files

> Compare config.dev.js and config.prod.js

Claude reads both and shows differences.

> Show me all the test files

Claude finds and summarizes test files.

Partial Reading

For large files:

> Show me the first 50 lines of server.js

> What's in the handleAuth function in auth.js?

Claude can focus on specific sections.

Creating Files

Simple Creation

> Create a file called hello.py that prints "Hello World"

Claude will: 1. Show you the proposed file content 2. Ask for permission to create it 3. Create the file

With Specifications

> Create a React component called Button with these props:
  - label (string)
  - onClick (function)
  - disabled (boolean, optional)

Claude generates appropriate code based on context.

Multiple Files

> Create a basic Express server with:
  - index.js (main server)
  - routes/api.js (API routes)
  - middleware/auth.js (auth middleware)

Claude creates the structure and files.

Editing Files

Direct Edits

> In utils.js, change the timeout from 5000 to 10000

Claude finds the line and makes the change.

Descriptive Edits

> Add error handling to the fetchUser function

Claude understands the function and adds appropriate try/catch.

Refactoring

> Rename the variable 'data' to 'userData' throughout auth.js

Claude updates all occurrences.

Adding to Files

> Add a new method called 'reset' to the Counter class

Claude finds the class and adds the method.

The Permission System

Claude asks before modifying your filesystem:

Claude wants to create file: src/utils/helpers.js

--- Proposed content ---
export function formatDate(date) {
  return date.toISOString().split('T')[0];
}
------------------------

Allow? (y/n/e)

Options: - y - Yes, make the change - n - No, don't make it - e - Edit the proposed change first

Trust Modes

For experienced users, you can adjust permissions:

> /config

# Set trust level for file operations

Practical Patterns

Pattern 1: Explore Then Edit

> Show me the config file

> Okay, change the port to 8080

Read first to understand context.

Pattern 2: Batch Operations

> Add TypeScript types to all functions in utils/

Claude processes multiple files.

Pattern 3: Find and Replace

> Find all uses of 'console.log' and replace with our logger

Claude searches and replaces across files.

Pattern 4: Template Creation

> Create a new component following the same pattern as UserCard.jsx

Claude reads the template and creates a similar file.

Working with Different File Types

Code Files

> Add JSDoc comments to all exported functions in api.js

Configuration

> Add a new script called "lint" to package.json that runs eslint

Documentation

> Update the README to include the new API endpoints

Data Files

> Add a new user to users.json with id 5, name "Alice"

Try It Yourself

Exercise: File Operations Practice

  1. Create a practice directory:

    mkdir file-practice && cd file-practice
    claude
    

  2. Create some files:

    > Create a calculator.py with add, subtract, multiply, divide functions
    

  3. Read and understand:

    > Explain what calculator.py does
    

  4. Make edits:

    > Add a power function to calculator.py
    
    > Add docstrings to all functions
    

  5. Create related files:

    > Create test_calculator.py with tests for each function
    

  6. Verify your work:

    > Show me both files side by side
    

Exercise: Refactoring

  1. Ask Claude to create a messy file:

    > Create a file called messy.js with a 50-line function that does
      too many things (fetch user, validate, format, save)
    

  2. Refactor it:

    > Refactor messy.js into smaller, focused functions
    

  3. Compare:

    > What improvements did we make?
    

Best Practices

Be Specific

# Vague
> Fix the file

# Specific
> In auth.js, the validateToken function returns undefined when
  the token is expired. It should throw an ExpiredTokenError instead.

Provide Context

> I'm using Express.js and MongoDB. Create a user registration
  endpoint that hashes passwords with bcrypt.

Verify Changes

> Show me what you changed in that last edit

Use Relative Paths

> Edit src/components/Header.jsx

Claude understands your project structure.

What's Next?

Now that you can work with files, learn how to prompt Claude effectively in 04-asking-questions.


Summary: - Claude reads files automatically when you reference them - Ask to create files, and Claude shows you what it'll write before doing it - Edit by describing what you want changed - no need for exact syntax - The permission system protects you from unwanted changes - Be specific about what files you mean and what changes you want


Learning Resources

NetworkChuck: AI in the Terminal - File Operations (3.6M subscribers)

Learn how Claude Code reads, creates, and modifies files directly on your filesystem with permission handling.

Additional Resources

Type Resource Description
🎬 Video Claude Code File Editing Guide All About AI - File operations deep dive
📚 Official Docs Security & Permissions File access controls documentation
📖 Tutorial DataCamp File Operations Refactoring and file editing examples
🎓 Free Course Anthropic Academy Official free courses
💼 Commercial GitHub Copilot Course AI file editing patterns