Week 3 of 12 🔍

Search & Navigation: Grep & Glob

การค้นหาและนำทาง: Grep และ Glob

搜索与导航:Grep 和 Glob

Search & Navigation: Grep & Glob

1. Introduction / บทนำ / 简介

การค้นหาโค้ดในโปรเจกต์ขนาดใหญ่เป็นสิ่งจำเป็น Claude Code มีเครื่องมือค้นหาที่ทรงพลังสองตัว: Grep (ค้นหาข้อความภายในไฟล์) และ Glob (ค้นหาไฟล์ตามรูปแบบชื่อ) ช่วยให้คุณนำทางโค้ดเบสได้อย่างรวดเร็ว
在大型项目中查找代码是必不可少的。Claude Code 提供两个强大的搜索工具:Grep(在文件内搜索文本模式)和 Glob(按文件名模式查找文件)。它们一起帮助你快速浏览任何代码库。
Finding code in a large project is essential. Claude Code provides two powerful search tools: Grep (search inside files for text patterns) and Glob (find files by name patterns). Together, they help you navigate any codebase quickly.

Fun Facts / ข้อเท็จจริงที่น่าสนใจ / 趣味知识

  • Grep supports full regex patterns like 'function\s+\w+'.
  • Glob uses patterns like **/*.ts to find all TypeScript files.
  • The Explore agent combines both tools for deep codebase research.
Painting of a man reading a paper through a magnifying glass
Searching by hand, 1895 — Lesser Ury, “Reader with Magnifying Glass”. Grep is faster. — Photo: Lesser Ury, Public domain, via Wikimedia Commons

2. Vocabulary / คำศัพท์ / 词汇

Click a card to flip it. คลิกการ์ดเพื่อพลิก 点击卡片翻面。

search
ค้นหา
搜索
To look for something
pattern
รูปแบบ
模式
A regular expression or glob to match
match
ตรงกัน
匹配
When a search finds a result
result
ผลลัพธ์
结果
What a search returns
directory
ไดเรกทอรี
目录
A folder containing files
glob
กลอบ
通配符模式
A pattern to match file names (e.g., *.js)
grep
เกรป
文本搜索工具
A tool to search text inside files
regex
เรกซ์
正则表达式
Regular expression — a search pattern
filter
กรอง
筛选
To narrow down results
codebase
โค้ดเบส
代码库
All the source code in a project
explore
สำรวจ
探索
To look through code to understand it
navigate
นำทาง
导航
To move through files and folders

3. Grammar Focus / ไวยากรณ์ / 语法焦点

Question Words (What, Where, How many) / คำถาม (What, Where, How many) / 疑问词(What、Where、How many)

Thai:
คำถาม (Wh-words) ใช้ถามข้อมูลเฉพาะ What = สิ่งของ, Where = สถานที่, How many = จำนวน, Which = ตัวเลือก ตัวอย่าง: 'What does Grep search for?' 'Where are the config files?'
中文:
疑问词(Wh-words)用来询问具体信息。What = 事物,Where = 地点,How many = 数量,Which = 选择。例如:'What does Grep search for?'(Grep 搜索什么?)'Where are the config files?'(配置文件在哪里?)
English:
Question words (Wh-words) are used to ask for specific information. What = things, Where = places, How many = quantity, Which = choice. Example: 'What does Grep search for?' 'Where are the config files?'
ไฟล์อะไรตรงกับรูปแบบ *.js?
哪些文件匹配 *.js 模式?
What files match the pattern *.js?
ไฟล์ configuration หลักอยู่ที่ไหน?
主配置文件在哪里?
Where is the main configuration file?
การค้นหาคืนผลลัพธ์กี่รายการ?
搜索返回了多少条结果?
How many results did the search return?

Section Check 1

Which tool searches inside files?

เครื่องมือใดค้นหาภายในไฟล์?

哪个工具在文件内部搜索?

Section Check 2

What does **/*.py match?

**/*.py ตรงกับอะไร?

**/*.py 匹配什么?

Section Check 3

Which question word asks about quantity?

คำถามใดถามเกี่ยวกับจำนวน?

哪个疑问词用来询问数量?

4. Tutorial Steps / ขั้นตอนการเรียนรู้ / 教程步骤

1
Search for text in files / ค้นหาข้อความในไฟล์ / 在文件中搜索文本
Ask: 'Find all files that contain the word error' — Claude uses Grep.

How to do it

  • Ask: Find all files that contain the word error.
  • Claude runs Grep and returns file names plus the matching lines with line numbers.
  • Grep searches inside files — use it when you know a word but not the file.
2
Find files by pattern / ค้นหาไฟล์ตามรูปแบบ / 按模式查找文件
Ask: 'Find all .json files in the config directory' — Claude uses Glob.

How to do it

  • Ask: Find all .json files in the config directory.
  • Claude uses Glob with a pattern like config/**/*.json.
  • * means "any name", ** means "any folder depth". Glob finds files by name, not content.
3
Use regex patterns / ใช้รูปแบบ regex / 使用 regex 模式
Ask: 'Search for function definitions matching function\s+handle' — Grep supports regex.

How to do it

  • Regex describes a text pattern, not an exact word.
  • Example: function\s+handle\s+ means "one or more spaces", so it matches function handle and function  handleClick.
  • You can just describe the pattern in English — Claude writes the regex for you.
4
Filter results / กรองผลลัพธ์ / 筛选结果
Grep can show file names only, line content, or match counts.

How to do it

  • Say what you want back: just the file names, show the matching lines, or count matches per file.
  • Fewer results are easier to read — start narrow, widen if you find nothing.
5
Use the Explore agent / ใช้ Explore agent / 使用 Explore agent
For complex searches, Claude can spawn an Explore subagent to research the codebase.

How to do it

  • For big questions like where is login handled in this project?, ask Claude to use the Explore agent.
  • It runs many searches at once and returns a short summary instead of raw results.
  • Your main conversation stays clean — the agent does the digging elsewhere.

5. Pattern Guide — Top 20 / คู่มือแพทเทิร์น 20 อันดับ / 模式指南 · 前 20

Glob matches file names and paths. Three symbols do all the work: * = any name inside one folder, ** = any depth of folders, ? = exactly one character.

Regex matches text inside files. It describes a pattern, not an exact word: \d = a digit, \s = a space, \b = a word edge, ^ = line start, $ = line end, | = or, + = one or more, ? = optional. A real dot must be written \.

Top 10 Glob patterns

PatternFinds
*.pyevery Python file in the current folder
**/*.pyevery Python file in any folder
src/*.jsJS files directly inside src/
src/**/*.jsJS files anywhere under src/
*.test.jstest files by their suffix
config/**/*.jsonJSON anywhere under config/
**/test_*files starting with test_ in any folder
*.mdevery Markdown file here
**/.env*env/config dotfiles anywhere
photos/202?.jpg? = one character: 2020.jpg … 2029.jpg

Top 10 Regex patterns

PatternMatches
errorthe word error anywhere in a line
^importlines that START with import
TODO|FIXMEeither word
\bclass\bthe whole word class only (not classroom)
function\s+\w+function, spaces, then a name
\d+one or more digits (7, 42, 2026)
"[^"]*"anything inside double quotes
\.png$lines that END with .png
user_?nameusername or user_name (? = optional _)
(GET|POST) /apiAPI lines with either verb

Tip: you never have to remember these — describe what you want in plain English and Claude writes the pattern. This table is for reading what Claude wrote back to you.

6. Hands-On Activity / กิจกรรมลงมือทำ / 动手练习

แบบฝึกหัด: ขอให้ Claude ค้นหาไฟล์ Python ทั้งหมดในโปรเจกต์ จากนั้นค้นหาไฟล์ที่มีคำว่า 'import'
练习:让 Claude 找出项目中所有的 Python 文件,然后搜索包含 'import' 这个词的文件。
Practice: Ask Claude to find all Python files in your project, then search for any file containing the word 'import'.

Step by step

  1. Start claude in a project that has some code files.
  2. Ask: Find all Python files in this project (this uses Glob).
  3. Ask: Which of those files contain the word import? (this uses Grep).
  4. Ask: Show me line numbers for the matches in one file.

✓ You did it right if: you get a file list, then matches with line numbers you can open and check.

📜 A complete search session / ตัวอย่างคำตอบ / 示例答案
> Find all Python files in this project
* Glob(**/*.py)
  app.py
  tests/test_app.py

> Which of those files contain the word import?
* Grep("import")
  app.py - 2 matches
  tests/test_app.py - 1 match

> Show the matching lines in app.py with line numbers
  1: import os
  2: import json

7. Reference Table / ตารางอ้างอิง / 参考表

Command / ToolEnglishภาษาไทย中文
GrepSearch text inside filesค้นหาข้อความภายในไฟล์在文件内搜索文本
GlobFind files by name patternค้นหาไฟล์ตามรูปแบบชื่อ按文件名模式查找文件
**/*.jsAll .js files recursivelyไฟล์ .js ทั้งหมดแบบ recursive递归匹配所有 .js 文件
Explore agentDeep codebase researchวิจัยโค้ดเบสเชิงลึก深入研究代码库

8. Interactive Lab / ห้องทดลอง / 互动实验室

Search a tiny fake project with real patterns — Grep for content, Glob for names. / ลองค้นหาด้วย Grep และ Glob / 用 Grep 和 Glob 实际搜索

Three fake files. Type a word (or tick regex) and watch Grep find it live.

9. Summary / สรุป / 总结

Grep ค้นหาภายในไฟล์ Glob ค้นหาไฟล์ตามชื่อ ใช้คำถาม (What, Where, How many) เพื่อถามเกี่ยวกับผลการค้นหา Explore agent จัดการงานวิจัยที่ซับซ้อน
Grep 在文件内部搜索。Glob 按文件名查找文件。用疑问词(What、Where、How many)来询问搜索结果。Explore agent 负责处理复杂的研究任务。
Grep searches inside files. Glob finds files by name. Use question words (What, Where, How many) to ask about search results. The Explore agent handles complex research tasks.

Homework / การบ้าน / 作业

1. ค้นหาคอมเมนต์ TODO ทั้งหมดในโปรเจกต์
2. ค้นหาไฟล์ทั้งหมดที่ลงท้ายด้วย .md
3. เขียนคำถาม 5 ข้อโดยใช้ Wh-words เกี่ยวกับโค้ดเบส
1. 搜索项目中所有的 TODO 注释。
2. 找出所有以 .md 结尾的文件。
3. 用疑问词写 5 个关于代码库的问题。
1. Search for all TODO comments in a project.
2. Find all files ending in .md.
3. Write 5 questions using Wh-words about a codebase.