Terminal Medium · Lesson 1 · On the house

find by name and type

Walk a tree with find -name and -type. Lesson 1 is free — on the house.

Lessons · Terminal Medium · Lesson 1 of 10 · On the house

find by name and type

Walk a tree with find -name and -type.

Illustration for Terminal
Lesson 1 is on the house.

find walks a directory tree and prints paths that match tests you give it. Start point first: find . means "from the current directory." find /tmp starts at /tmp. Unlike a plain glob, find recurses into subdirectories by default.

-name '*.log' matches the basename against a shell-style pattern (quote it so the shell does not expand the star before find runs). -iname is the case-insensitive form. Patterns apply per path component name, not the full path string.

-type f keeps regular files; -type d keeps directories; -type l keeps symlinks. Combine tests with implied AND: find . -type f -name '*.py' means file AND name match. Print is the default action — matching paths go to stdout, one per line.

Scope matters: find src -name main.go only searches under src. find . -name main.go searches the whole tree under .. Prefer a tight start path over filtering a giant dump later.

Misses: forgetting quotes around *.log so the shell expands it in the cwd first, using -name when you meant a full-path match (-path / -wholename), or assuming -type f includes directories.

find-name-type

find . -type f -name '*.log'
find . -type d -name 'src'
find /tmp -type f -iname 'readme*'
find . -type f -name '*.py'
Match this when you type.

Quiz

What does find . start from?

Quiz

Why quote -name '*.log'?

Quiz

What does -type f select?

Quiz

What does find src -name main.go search?

Check

Match the still for: find by name and type.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.