Find names or search contents

Lesson 9 of 30 · Working with Linux

Two questions need different tools: “Where is a file with this name?” and “Which lines contain this text?” In the practice directory, find answers the first by walking directory entries:

find . -name 'field.txt'
find . -type f -name '*.txt'

The starting point . means the current directory. -name compares each entry’s name; -type f limits the second search to regular files. In the pattern, * matches zero or more characters. Quotes pass the pattern to find rather than letting Bash expand it first. Both commands include ./field.txt if the setup succeeded. Additional results and ordering depend on what else is in the directory.

grep, by contrast, searches text inside files and prints matching lines:

grep -F 'Maple' field.txt
grep -iF 'maple' field.txt

The first prints Maple; the second prints both Maple and maple leaf. -F treats the search as fixed text rather than a regular-expression pattern; -i ignores case. With multiple file arguments, grep normally prefixes results with their names. grep -rF 'River' . searches recursively under the current directory; keep its starting point limited to the practice space.

A search without a match normally prints nothing and grep returns status 1; an error is different. Access errors or unreadable directories can leave a search incomplete, so no result alone does not prove that an entry or line is absent.