Finding files with find
Quick answer
find walks a directory tree and tests each entry against an expression. Search from a small known directory rather than the entire system. You can filter by name, type, size or other metadata.
Prepare the practice environment
Use the chapter fixture described in lab-fixtures/search-transform/README.md. Run setup as a normal user in a disposable Linux environment, enter the printed directory and check pwd. Every example assumes a fresh fixture. Commands omit the terminal prompt; outputs below are not commands.
How it works
In GNU find, give a starting path, then tests and actions. -type f selects regular files; -name matches the base name using a glob, not regex; -maxdepth bounds traversal depth (GNU extension); adjacent tests are AND by default. -print emits a newline-delimited pathname for display. Traversal order is not guaranteed. locate uses an optional index that may be stale; find checks the live tree. locate is not installed here and is not required.
The starting directory has depth zero, its direct children depth one, and their children depth two. With -maxdepth 1, a matching file under a subdirectory is outside the traversal boundary. Build a read-only selection first and inspect the printed paths before considering a separate action; a listing is not a guarantee against later filesystem changes.
Try the examples
The quoted pattern is evaluated by find, and sort stabilizes display order.
Expected stdout:
docs/daily report.txtdocs/notes.txtdocs/sub/old.txtThe nested file is not selected.
Expected stdout:
docs/daily report.txtdocs/notes.txtThe starting directory also satisfies the directory test.
Expected stdout:
docsdocs/subCommon mistakes
Quote *.txt so the shell does not expand it before find sees it. Do not add -delete or destructive -exec actions to an unreviewed search. Newline-separated display is not a safe filename transport format.
Practice
List only .log files under docs and explain why a matching directory would not pass -type f. Expected: docs/sub/audit.log.
Related references
Study: find, locate. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Finding files with find — Knowledge Check
Apply the lesson concepts to the controlled text fixture.
Which test selects regular files?
Why quote *.txt?
What does -maxdepth 1 exclude?
Why may locate miss a new file?