Searching File Trees with Find
Introduction: From Globs to Find
A simple glob such as *.log selects names in the current directory. Globs can also include path components, such as app/*.log or */*.log, to reach other levels. find adds recursive traversal from a starting directory and tests for properties such as file type and size, which helps when we do not know where a file is located.
find solves that problem. Instead of matching names where you're standing, it walks an entire directory tree and picks out files based on their attributes — their name, their type, their size, and how deep they sit. This lesson covers all four, plus one more skill: turning a long list of matches into a single countable number using a pipe.
The Shape of a Find Command
Every find command follows the same basic shape: you name a starting point — the directory to search — and then list tests that describe what you're looking for. Put simply, it's find WHERE-TO-LOOK WHAT-TO-LOOK-FOR. If you leave off the tests entirely, find doesn't filter anything — it just lists every file and folder it can reach below that starting point, all the way down.
This matters because it flips the mental model you used with globbing. A glob asks, "What matches in front of me right now?" find asks, "Starting from this folder, what matches anywhere underneath it?" That's a much bigger question, and the syntax reflects it: the path comes first, so find knows where to begin descending before any test narrows down what counts as a hit.
Building a Tree to Search
Let's build a small tree to search so the rest of this lesson has something real to point at.
That builds this layout inside tree/:
Three files sit one level down, directly inside app/ and docs/ — app/app.log, docs/plan.txt, and docs/readme.txt — and one more sits a level deeper still, inside app/cache/. docs/cache/ is an empty folder — no files inside it at all, but it still counts as something find can see.
