Finding Files with find
Searching for Files by Name
Imagine you know a file exists somewhere in your project, but you have no idea which folder it’s in. Maybe you remember it’s called config.json, or you know it ends in .log. The find command is built for exactly this.
This is one of the most common commands AI coding tools run. When your AI assistant needs to understand a project's structure or locate a specific file,
find is usually its first move.
The Basic Pattern
The find command follows this structure:
find [where to look] [what to match]
Try it now: Type find ~/projects/my-app -name "*.js"
This searches inside the ~/projects/my-app directory (and all its subdirectories) for any file whose name ends with .js.
Let’s break that down:
| Part | Meaning |
|---|---|
find |
The command |
~/projects/my-app |
Where to start searching |
-name "*.js" |
Match files named anything ending in .js |
The . (dot) is also a common starting point — it means “start searching from right here”:
find . -name "*.js"
Glob Patterns in -name
The * in "*.js" is called a glob pattern (or wildcard). It means “match anything.” Here are some common patterns:
| Pattern | Matches |
|---|---|
"*.js" |
Any file ending in .js |
"*.log" |
Any file ending in .log |
"config.*" |
Any file named config with any extension |
"*.test.js" |
Any file ending in .test.js |
Important: Always put quotes around the pattern. Without them, your shell might try to expand the * before find gets a chance to use it.
Filtering by Type
Sometimes find returns both files and directories. You can narrow it down:
-type f— only files-type d— only directories
Try it now: Type find ~/projects/my-app -name "*.log" -type f
This finds only files ending in .log, skipping any directories that might happen to have .log in their name.
Try this too: Type find ~/projects/my-app -type d
That lists every directory in the project — a quick way to understand a project’s structure.
Practical Examples
Here are some real-world uses you’ll see AI tools run:
find . -name "package.json" -type f
Find all package.json files (useful in monorepos with multiple packages).
find ~/projects/my-app -name "*.test.js" -type f
Find all test files in a project.
find . -name "*.log" -type f
Find all log files starting from the current directory.
A Word of Caution: -delete
The find command has a -delete flag that removes every file it matches.
find . -name "*.log" -delete
The
-delete flag permanently removes files with no confirmation and no trash can. Always run the find command without -delete first to see what it would match. If an AI tool suggests a command with -delete, double-check the results before approving it.
Practice
Try these in the terminal:
find ~/projects/my-app -name "*.js" -type f— find all JavaScript filesfind ~/projects -type d— see the directory structure of all your projectsfind ~/projects/my-app -name "*.json"— find all JSON config filesfind ~/Documents -name "*.txt" -type f— find text files in Documents