Lesson 37.2 · 2 of 5 · 5 min read · Reviewed 24 Nov 2026
Bash, Grep and Glob
By the end, you can
- Choose between Bash, Grep and Glob.
- Search a codebase efficiently.
Glob: find files by name
**/*.test.ts, src/api/**/*.py, **/Dockerfile. Fast, and returns paths only.
Grep: search inside files
Regular-expression search across the codebase. Options let it return matching lines with context, or just the file names. Use it to find callers, config keys, TODOs or error messages.
Bash: run commands
Tests, builds, linters, git and package managers. It’s the most powerful tool, so it’s the one to scope most carefully:
"allow": ["Bash(pnpm test:*)", "Bash(git diff:*)"],
"deny": ["Bash(curl:*)", "Bash(rm -rf:*)"]
An efficient search habit
- Glob to find candidate files.
- Grep to find the exact spots.
- Read only the lines that matter.
That’s much cheaper than reading whole files to look for something.
Key points
- Glob finds files by name pattern, such as **/*.test.ts.
- Grep searches file contents with regular expressions, and can return only matching file names.
- Bash runs shell commands such as tests and builds; scope it with permission rules.
Search a repo with glob and grep
Find where a setting lives using glob, then grep, then a narrow read, and measure how much less text that reads than opening every file.
You will have: A script that prints the matching file:line hits and the characters read by each approach.
Steps
- Read the fake repository: a dictionary of paths to file contents.
- Write glob(pattern) with fnmatch to list candidate Python files under src/.
- Write grep(regex, paths) returning (path, line number, line) for each match.
- Read only two lines either side of each hit.
- Compare characters read against reading every file in full.
Done when
- glob finds only the files under src/ ending in .py.
- grep finds both places retry_delay is set, with line numbers.
- The narrow approach reads at least 5 times fewer characters than reading everything.
Open in the Playground No API key needed.
Starter code (Python)
import re
from fnmatch import fnmatch
repo = {
"src/payments/client.py": "import time\n\nretry_delay = 2.0\n\ndef charge(card):\n return True\n",
"src/payments/settings.py": "# settings\nTIMEOUT = 30\nretry_delay = 0.5 # overrides client default\n",
"src/web/views.py": "def index():\n return 'hello'\n" * 40,
"docs/guide.md": "Use retry_delay to tune retries.\n" * 20,
"tests/test_client.py": "def test_charge():\n assert True\n" * 30,
}
def glob(pattern):
# TODO: return paths matching the pattern
return list(repo)
def grep(regex, paths):
# TODO: return [(path, line_no, line)] for lines matching regex
return []
candidates = glob("src/**.py")
hits = grep(r"^retry_delay\s*=", candidates)
print("candidates:", candidates)
print("hits:", hits)
Show a solution
import re
from fnmatch import fnmatch
repo = {
"src/payments/client.py": "import time\n\nretry_delay = 2.0\n\ndef charge(card):\n return True\n",
"src/payments/settings.py": "# settings\nTIMEOUT = 30\nretry_delay = 0.5 # overrides client default\n",
"src/web/views.py": "def index():\n return 'hello'\n" * 40,
"docs/guide.md": "Use retry_delay to tune retries.\n" * 20,
"tests/test_client.py": "def test_charge():\n assert True\n" * 30,
}
def glob(pattern):
return [p for p in repo if fnmatch(p, pattern)]
def grep(regex, paths):
rx = re.compile(regex)
return [(p, i, line) for p in paths for i, line in enumerate(repo[p].splitlines(), 1) if rx.search(line)]
def read_around(path, line_no, ctx=2):
lines = repo[path].splitlines()
return "\n".join(lines[max(0, line_no - 1 - ctx): line_no + ctx])
candidates = glob("src/**.py")
hits = grep(r"^retry_delay\s*=", candidates)
print("candidates:", candidates)
for p, n, line in hits:
print(f"{p}:{n}: {line}")
narrow = sum(len(p) for p in candidates) + sum(len(read_around(p, n)) for p, n, _ in hits)
everything = sum(len(c) for c in repo.values())
print(f"chars read: narrow={narrow} everything={everything} ratio={everything / narrow:.0f}x")