Skip to main content

Understanding File Operations Safely

Before You Move Furniture, Make a Plan

Imagine reorganizing your home. You wouldn't rearrange furniture without knowing:

  • What's moving where?
  • Can I move it back if I change my mind?
  • What if something breaks?

File operations work the same way. Your AI companion can copy, move, and delete files. Your job is to understand the plan, ask safety questions, and verify it worked.

This lesson teaches you to apply the 5-step safety pattern from Lesson 2 to real file operations. By the end, you'll be confident requesting file operations and supervising them safely.


Example 1: Copying Files Safely (Full Safety Pattern)

Here's a real conversation showing all 5 steps of the safety pattern applied to a file operation.

Step 1: You Ask (plain language)

You: "I want to back up my project code before I start making changes. How would you do that safely?"

Step 2: AI Explains (plan before execution)

Agent: "Here's my approach. First, I'll show you the project files. Then I'll create a timestamped backup folder like project-backup-2025-11-02. I'll copy all your code files into it. Finally, I'll verify the backup worked by showing you the contents. This way, if something goes wrong with your changes, you can restore from this backup. Sound good?"

Step 3: You Understand (confirm)

You: "Yes, I understand. You're creating a copy, not moving the files. My original code stays in place, and the backup is separate. That's what I want."

Step 4: You Verify (ask safety questions)

You: "Before you create the backup, tell me: Will you copy hidden files too? And how big will the backup be—is there enough disk space? And one more thing: if I need to restore from this backup, how would I do that?"

Agent: "Good questions. I'll exclude hidden files like .git to keep the backup focused on your code files. The backup will be roughly the same size as your project. To restore, you'd just copy files from the backup folder back to your original location. And yes, we have plenty of disk space."

Step 5: Execute (run commands)

Agent: "Now I'll create the backup."

$ mkdir project-backup-2025-11-02
$ cp -r src/ project-backup-2025-11-02/src/
$ cp -r tests/ project-backup-2025-11-02/tests/
$ cp README.md project-backup-2025-11-02/README.md
$ ls -la project-backup-2025-11-02/
total 48
drwxr-xr-x 5 user staff 160 Nov 2 10:00 src/
drwxr-xr-x 3 user staff 96 Nov 2 10:00 tests/
-rw-r--r-- 1 user staff 2048 Nov 2 10:00 README.md

Agent: "Backup complete. I've created the backup folder with all your essential code. Your original files are unchanged. If something goes wrong, you can restore from this backup."


Understanding Destructive Operations

Notice what happened:

The copy operation is safe because:

  1. It creates a new copy; original files stay in place
  2. You understood the plan before execution
  3. You asked about space, file selection, and recovery
  4. The verification showed the backup exists

But not all file operations are this safe. Let's look at the riskier ones.


Example 2: Moving Files (Requires More Caution)

Moving files is different from copying. When you move, the file changes location and no longer exists in the original place.

You: "I want to reorganize my project. Move all Python files into a src folder. But I'm nervous about this."

Agent: "Smart to be nervous. Moving is riskier than copying. Here's what I'll do:

  1. First, I'll create a backup of your current structure (so you can undo if needed)
  2. Then I'll move Python files to src
  3. Then I'll verify the move worked by showing before and after

That way, if something goes wrong, we can restore from the backup. Shall we proceed?"

You: "Yes, but first create the backup. And tell me: will this move ALL Python files, or just the ones in the current folder? What if I have Python files in subfolders?"

Agent: "I'll use a command that finds Python files at any depth and moves them. I'll show you exactly which files will move before we execute."

$ mkdir backup-before-reorganize
$ cp -r * backup-before-reorganize/ 2>/dev/null
$ ls backup-before-reorganize/

$ find . -name "*.py" -type f
./app.py
./config.py
./utils.py
./tests/test_app.py
./tests/test_utils.py

You: "Perfect. I can see all 5 Python files. Move them to src."

$ mkdir -p src
$ find . -name "*.py" -type f -exec mv {} src/ \;
$ ls src/
app.py
config.py
utils.py
test_app.py
test_utils.py

Agent: "Done. All Python files are now in src. Your original locations are empty. The backup folder still contains the original structure if you want to undo."


Example 3: Deleting Files (Most Risky—Always Backup First)

Deletion is permanent (or at least harder to undo). Every deletion must follow the safety pattern strictly.

You: "I have old test files that I don't need anymore. I want to delete test_old_auth.py, test_old_db.py, and test_old_utils.py."

Agent: "Before I delete anything, let me show you what exists and we'll verify you want to delete exactly those files."

$ ls test_old*
test_old_auth.py
test_old_db.py
test_old_utils.py
test_old_config.py

You: "Wait! I see test_old_config.py in the list. That wasn't supposed to be there. Don't delete that one."

Agent: "Good catch. So you want to delete these three: test_old_auth.py, test_old_db.py, test_old_utils.py. But NOT test_old_config.py. I'll delete only those three by name. And I'll move them to a Trash folder first, so if you change your mind within 24 hours, we can recover them."

$ mkdir -p .trash-deleted
$ mv test_old_auth.py test_old_db.py test_old_utils.py .trash-deleted/
$ ls test_old*
test_old_config.py

Agent: "Deleted. The three files are in a trash folder. Your original test_old_config.py is still there. You can safely delete the trash folder later, or recover from it if you change your mind."


Why Safety Matters: The Three Operations

Let's compare the three operations:

OperationRisk LevelExampleRecovery
CopyLowcp -r project backup/Safe—original untouched
MoveMediummv old-code/ archived/Moderate—creates new location
DeleteHighrm test_file.pyDifficult—file gone (may be in trash)

Before ANY operation, especially move and delete, you MUST:

  1. Ask your AI to show you what will be affected
  2. Understand the command and flags
  3. Ask "Can we undo this?"
  4. Request a backup if it's a destructive operation
  5. Have your AI show before/after verification

Exercise 1: Safety Dialogue Evaluation

Read this dialogue and identify which steps of the safety pattern are present or missing.

Dialogue:

You: "I want to delete old backup files to save space."

Agent: "I'll delete the backup folder to free up space."

You: "Okay, do it."

Agent: Deletes the backup folder.

Which steps are missing?

  • Step 1 (Ask): ✓ Present
  • Step 2 (Explain): ✗ MISSING — Agent didn't explain which folder or commands
  • Step 3 (Understand): ✗ Unclear — You didn't verify understanding
  • Step 4 (Verify): ✗ MISSING — No safety questions asked
  • Step 5 (Execute): ✓ Present (but too early!)

Problems with this dialogue:

  • You didn't see what files would be deleted
  • There's no backup or undo option
  • You didn't ask "Can I recover these files?"
  • The operation is too risky given the lack of verification

Better dialogue:

You: "I want to delete old backup files to save space."

Agent: "Before we delete anything, let me show you what backup files exist and their sizes. That way you can confirm exactly what to delete."

Agent: Lists backup files with sizes

You: "I can see backup-2024-01 (500MB), backup-2024-02 (480MB), backup-2025-01 (600MB).
Delete backup-2024-01 and backup-2024-02. But keep backup-2025-01."

Agent: "Got it. I'll move those two to a trash folder first, then delete them.
If you change your mind within 7 days, we can recover them."

Agent: Moves files to trash and verifies the operation.

You: "Perfect. You saved 980MB of space, and I kept the recent backup."

Exercise 2: Asking Verification Questions

Your AI suggests: "I'll reorganize your project by renaming three folders and moving files between them."

Write 3 clarifying questions you MUST ask before your AI executes this operation:

  1. Example: "Show me the before-and-after structure. I want to see which files move where."

  2. Your question:

  3. Your question:

  4. Your question:


Good verification questions might include:

  • "Show me the exact before and after structure. Which files go where?"
  • "Will this break any imports or scripts that reference the old folder names?"
  • "Can we undo this if it breaks something?"
  • "Should we backup the current structure before making changes?"
  • "How will we verify the reorganization worked?"

Exercise 3: Suggest Verification Commands

You've asked your AI to copy important project files to a backup folder. The operation completed. Your AI shows:

$ cp -r /Users/mjs/project /Users/mjs/project-backup

What verification commands should your AI run next to confirm the backup succeeded?

Write the commands your AI should execute to verify:

  1. The backup folder was created
  2. All files were copied
  3. The original files are untouched

Model answer:

$ ls -la project-backup/
$ du -sh project/ project-backup/
$ diff -r project/ project-backup/

These commands:

  • Show the backup folder exists and its contents
  • Compare folder sizes (should be approximately equal)
  • Show any differences (should be none if copy was successful)

Formative Assessment: File Operations

Answer these questions:

Question 1: Which file operation is MOST risky?

  • A) Copy
  • B) Move
  • C) Delete

Correct: C. Delete is permanent and hardest to undo.

Question 2: Before moving files, what's the FIRST safety step?

  • A) Ask your AI to move them immediately
  • B) Create a backup of the current state
  • C) Rename the files

Correct: B. Always backup before destructive operations.

Question 3: Why is it important to see the list of files BEFORE your AI deletes?

  • A) You need to count them
  • B) To verify you're deleting exactly the right files, not accidental extras
  • C) Delete operations always show a list automatically

Correct: B. Seeing the exact files prevents accidental deletion.


Summative Assessment: Complete File Operation with Safety

Have a real conversation with your AI where you:

  1. Ask your AI to help with a file operation (copy, move, or organize files—something real from your project)
  2. Have your AI explain the plan with specific commands
  3. Ask at least 3 verification questions (about affected files, backup strategy, undo capability)
  4. Have your AI show the before state (list the files)
  5. Approve the operation
  6. Have your AI execute and verify (show after state to confirm success)

Success criteria:

  • You followed all 5 steps of the safety pattern
  • You asked clarifying questions about affected files
  • You verified the operation succeeded with before/after snapshots
  • You could explain why this approach is safer than just saying "yes, do it"

Try With AI

Tool: Claude Code, ChatGPT Code Interpreter, Gemini CLI, or your preferred AI companion

Setup: You're going to request a file operation and supervise it using the safety pattern.

Prompt 1: Organize With Safety

Copy and paste this prompt:

I want to organize my project files.
Before you execute anything, walk me through all 5 safety steps:
1. Ask: Clarify what I want (I want to move all markdown files to a docs folder)
2. Explain: Show the exact commands you'll run
3. Understand: Confirm I understand the plan
4. Verify: I'll ask you questions about affected files
5. Execute: Only then run the commands

Take your time. I want to understand each step.

Expected Outcome:

  • Your AI clearly labels each step
  • You see the commands before they execute
  • You have a chance to ask questions
  • The operation succeeds and is verified

Prompt 2: Backup Challenge

I'm nervous about moving files because I don't want to lose anything.
Design a backup strategy that:
1. Preserves the current state
2. Allows me to undo the operation within 24 hours
3. Saves space if possible

Show me the commands you'd use. Don't execute yet—just explain.

Expected Outcome: Your AI suggests a practical backup approach (like moving original files to a trash folder, or creating a dated backup)

Prompt 3: Verification Practice

After you complete a file operation, show me how to verify it worked.
Include commands that show:
1. The new file locations exist
2. Files weren't corrupted
3. Nothing was accidentally lost

Don't execute—just show me what verification would look like.

Expected Outcome: Your AI demonstrates verification techniques (directory listings, file counts, integrity checks)