A filename extension does not establish an entry's type. A directory can be
named archive.txt, and the file you need can be several directories below
your current location. To locate the intended entries, choose both where the
search may walk and which properties an entry must satisfy.
You will build one small owned tree, print matching paths, and compare name criteria with file-type criteria. Then you will narrow a search to one child without including similar files in its sibling. Every search in this lesson prints evidence; none removes files or executes another command on a match.
1. Build a small tree with deliberate near-matches
Complete Search text with literal and basic patterns first. Work in the checked Ubuntu guest as its ordinary Bash user. Run each line separately and inspect its result before continuing. Stop on unexpected errors; the deliberately missing-root observation is explained before it runs.
Use the taught identity and command-origin checks:
id -u
id -un
ps -p $ -o comm=
type printf set mkdir find cat ls rm rmdir
Require a nonzero user ID, the expected ordinary username and Bash. printf
and set must be Bash builtins; find and the other utilities must resolve
to their expected guest executables. Preserve an understood color-only listing
alias, and reconcile any other unexpected alias, function or origin before use.
Use the taught read-only option inspection:
set -o
Require errexit off for the planned unsuccessful observation. Preserve and
reconcile a different setting; no shell option change is part of the task.
The exercise owns ~/linux-practice/locate-files-by-criteria/. Inspect its
parents and prospective name without trailing slashes:
ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/locate-files-by-criteria
Require ordinary parent directories, shown with leading d, owned by your
recorded user. Stop on a link, unexpected owner or unrecognized path. The
exercise name must be absent for a fresh start; use the resume section if it
already exists.
Only after confirming absence, create and inspect this directory:
mkdir ~/linux-practice/locate-files-by-criteria
ls -ld ~/linux-practice/locate-files-by-criteria
Require the expected ordinary directory and owner before entering:
cd ~/linux-practice/locate-files-by-criteria
pwd
ls -a
Require the recorded shared-root path followed by /locate-files-by-criteria
and an inventory containing only . and .. before creating children.
Create named directories before their files
The taught mkdir can receive several explicit directory names. Create these
three children and inspect their identities:
mkdir reports notes archive.txt
ls -ld -- reports notes archive.txt
All three must be ordinary directories owned by your user. The .txt suffix
on archive.txt is deliberate; its name does not turn it into a text file.
Only after those checks, create and inspect the nested child:
mkdir reports/drafts
ls -ld -- reports/drafts
Require its expected ordinary identity. Check the fresh inventories:
ls -a
ls -a reports
ls -a reports/drafts
ls -a notes
ls -a archive.txt
The root should contain only the three named children and dot entries.
reports should contain only drafts and dot entries. The other three
inspected child locations must be empty. Stop if a planned file name already
exists or an unexpected entry appears.
Supply seven one-line files
The taught printf '%s\n' writes a supplied string and newline. Each > below
creates an absent file in the verified tree. These inputs are synthetic and
all destination names are explicit; do not write over an existing file or link.
printf '%s\n' 'Summary report' > report.txt
printf '%s\n' 'Delivery report' > reports/report.txt
printf '%s\n' 'Draft report' > reports/drafts/report.txt
printf '%s\n' 'Report guide' > reports/guide.md
printf '%s\n' 'Draft checklist' > reports/drafts/checklist.md
printf '%s\n' 'Notes guide' > notes/guide.md
printf '%s\n' 'Hidden reminder' > notes/.hidden.txt
The leading dot in .hidden.txt makes it a hidden name for ordinary ls
listings; it is still a regular file. The taught ls -a includes it.
Inspect every created file's type, link count and owner:
ls -ld -- report.txt reports/report.txt reports/drafts/report.txt reports/guide.md reports/drafts/checklist.md notes/guide.md notes/.hidden.txt
Require leading -, one link and the expected owner for each. Read each file
separately and compare its complete one-line content:
cat report.txt
cat reports/report.txt
cat reports/drafts/report.txt
cat reports/guide.md
cat reports/drafts/checklist.md
cat notes/guide.md
cat notes/.hidden.txt
The expected mapping is:
| Relative path | Type and supplied content |
|---|---|
report.txt | Regular file: Summary report |
reports/report.txt | Regular file: Delivery report |
reports/drafts/report.txt | Regular file: Draft report |
reports/guide.md | Regular file: Report guide |
reports/drafts/checklist.md | Regular file: Draft checklist |
notes/guide.md | Regular file: Notes guide |
notes/.hidden.txt | Regular file: Hidden reminder |
archive.txt | Empty directory; no file content |
Keep all these inputs unchanged until the independent work is complete.
missing-branch is intentionally absent for a later diagnostic. No result
file needs creating; the searches print paths to the terminal.
2. Separate the starting root from the criteria
find walks a directory tree starting at the path you supply and evaluates
criteria for the encountered entries. It considers the starting entry itself
as well as descendants. The search root sets the boundary of the walk;
the criteria decide which entries within that boundary qualify.
We explicitly use -P before the starting path. It is GNU find's default
non-following policy for symbolic links: it does not follow a linked directory
to search its target. The fixture deliberately contains no links, and the
root/parent identity checks remain necessary. A non-following option is not
permission to search an unrecognized starting path.
In the following form:
.is the current directory, already verified as the owned exercise root.-name '*.txt'tests the entry's basename, the final name component rather than its full path, against the quoted filename pattern.*in this find pattern matches zero or more characters; the.txtsuffix is literal. The search checks names, not file contents.-printexplicitly prints the qualifying path. It does not change the entry.
The single quotes ensure Bash passes *.txt as one pattern argument for
find to interpret. These are filename-pattern rules inside find, not grep's
regular-expression dot and anchor rules.
Predict the path set before running the first name-only search:
find -P . -name '*.txt' -print
Expect these five paths, in any order:
./report.txt
./reports/report.txt
./reports/drafts/report.txt
./notes/.hidden.txt
./archive.txt
The ./ prefix records the supplied . starting path. Traversal order is
not promised; compare membership and complete path names rather than positions
in the printed output.
Two observations matter. First, the name test included archive.txt because
it did not ask for a regular file. Second, find's -name wildcard can match
a leading dot, so the hidden note is included. This differs from the bounded
ordinary Bash filename-expansion defaults taught earlier. Quoting determines
which program receives and interprets the pattern.
3. Require the right entry type as well as its name
-type f requires a regular file; -type d requires a directory. The letters
are the type values for this criterion, not separate command options.
Consecutive criteria in these simple expressions act as AND: both must
succeed before the following -print action runs.
Predict which earlier result will disappear when the type is added:
find -P . -type f -name '*.txt' -print
Expect this four-path set, in any order:
./report.txt
./reports/report.txt
./reports/drafts/report.txt
./notes/.hidden.txt
The directory is excluded by -type f, while the hidden regular file still
qualifies. Equal-looking suffixes did not establish equal file types.
Now select the opposite type with the same name condition:
find -P . -type d -name '*.txt' -print
Expect only ./archive.txt. Neither command changed the source tree. Reading
the types separately helps explain the first overbroad name-only result.
Quoting prevents a different program from choosing the pattern
If you omit the quotes around *.txt, Bash can expand it before find starts.
In this fixture the current directory contains names such as report.txt
and archive.txt, so under the earlier ordinary matching defaults it can
supply multiple path arguments where you intended one pattern. This may cause
an expression error or an unintended search. With only one matching name in
a different directory, the mistake can silently narrow the request instead.
Do not remove the quotes as a repair for missing results. Inspect which starting path and criteria you actually supplied. Preserve shell settings; no option changes or deliberately broken command are needed to explain the quoting boundary.
4. Changing the root changes where find may look
From the same verified exercise directory, use the child reports as the
explicit starting root:
find -P reports -type f -name '*.txt' -print
Expect this two-path set, in any order:
reports/report.txt
reports/drafts/report.txt
The root-level report.txt and the note under notes are outside this starting
tree. A broader name pattern cannot bring those siblings into the walk.
The printed paths begin with reports, reflecting the supplied starting path;
that formatting difference does not mean the nested files were moved.
Compare the two independent choices:
| Change | What changes |
|---|---|
Keep . and narrow -name | Criteria change for entries throughout the same owned tree. |
Keep the same criteria and change . to reports | The walk starts inside that child, excluding siblings and the root-level file. |
Check the actual starting path before adjusting a pattern to compensate for an omitted branch. Every example remains inside the owned exercise tree.
5. No matching paths is not an unsuccessful search
GNU find's status convention differs from the grep no-match observation in
the previous lesson. A successfully completed find can print no matching paths
and still return 0. A traversal or expression error returns a nonzero status;
read the diagnostic instead of treating it as an empty successful result.
The supplied reports tree has no .csv files. Predict the output and inspect
the already taught status parameter immediately afterward:
find -P reports -type f -name '*.csv' -print
printf 'find status: %s\n' "$?"
Expect no paths and status 0. This is a completed search of that bounded
tree with no qualifying entry, not proof about every directory on the guest.
Another command before the status print would replace $?.
Confirm missing-branch is absent in ls -a. Then observe the intentionally
invalid starting root:
find -P missing-branch -type f -name '*.txt' -print
printf 'find status: %s\n' "$?"
Expect a diagnostic naming the unavailable root and a nonzero status. Exact wording, language and the particular nonzero number are not the match-set contract. The intended tree could not be walked; this is different evidence from the completed no-match search. Do not create the missing branch merely to silence the planned error.
Diagnose root, type and name separately
| Observation | Likely distinction to inspect | Safe next check |
|---|---|---|
A directory appears among .txt results. | A suffix test did not require regular files. | Inspect its metadata and the presence of -type f. |
| A known sibling is absent. | It may be outside the selected root. | Compare the complete source tree with the actual starting path. |
| The hidden note appears. | Find's name wildcard can match its leading dot. | Confirm the exact basename and type; do not import Bash's dotfile-matching default into find. |
| Unexpected path arguments or an expression error appear. | Bash may have expanded an unquoted pattern first. | Check quotes around the one -name pattern before changing criteria. |
| Matching paths arrive in a different order. | Traversal order is not the expected result contract. | Compare the path set and exclude missing or extra entries. |
| No paths appear. | A valid empty match set and a failed walk differ. | Read stderr and the immediate status, then inspect the bounded root. |
Independent task: only report Markdown files
Keep the complete fixture unchanged. Recheck the exact exercise location,
ordinary reports/reports/drafts identities, and the supplied file contents.
The question is: which regular Markdown files are beneath reports, including
its nested child, while excluding the similarly named file under notes?
Before opening the answer, choose an explicit starting root and combine type
and name criteria. Print the qualifying paths only. Predict both paths and
explain why the sibling notes/guide.md must be excluded. Do not move into a
different directory to make a guessed command happen to work.
After execution, compare the full path set with the supplied tree, inspect the corresponding file contents, and confirm that no file or directory was changed. Explain how widening the pattern differs from widening the root.
Success: exactly reports/guide.md and
reports/drafts/checklist.md qualify, in either order; both are regular files,
the sibling is excluded by the root, and all fixture contents remain intact.
Graduated hints
- The boundary is part of the requirement. Start in the
reportschild rather than searching the whole exercise tree and hoping the name excludes siblings. - A Markdown suffix alone does not prove an entry is a regular file. Combine the two tests with the simple implicit AND used in the guided example.
- Supply the single quoted pattern
'*.md'to-name, retain-type f, and finish with the taught-printaction.
Answer and Reasoning
From the verified exercise root, with the unchanged owned child identities, one valid search is:
find -P reports -type f -name '*.md' -print
Its path set is:
reports/guide.md
reports/drafts/checklist.md
reports bounds the traversal; -type f rejects directories; -name '*.md'
checks the literal suffix using find's filename-pattern rules. Both tests
must pass before -print. The quotes deliver one unexpanded pattern argument.
The rule reaches the nested child but does not reach the sibling notes.
Inspect the selected entries and their full contents:
ls -ld -- reports/guide.md reports/drafts/checklist.md
cat reports/guide.md
cat reports/drafts/checklist.md
Require the expected ordinary owned single-link files containing Report guide
and Draft checklist. Also inspect the excluded sibling's known content:
cat notes/guide.md
It still contains Notes guide; exclusion did not remove or change it.
An equivalent explicit owned starting path is acceptable if it reaches the
same child and returns the same underlying two files. Textual prefixes can
differ with that starting path; membership, type and scope are what matter.
Using . instead of reports with the same Markdown criterion would also
reach the sibling's Markdown file. Using reports with a broader pattern
would include more entries within reports, but could not reach the sibling.
That explains why a root error is not repaired by changing only the name test.
For the earlier no-match check, find completed the valid .csv search with
status 0, unlike grep's status 1 for no selected text lines. A missing
starting root produced a diagnostic and nonzero status. Command-specific
status meanings must be interpreted with the actual search evidence.
Cleanup after all searches
Finish the independent checks before removing anything. Reinspect the shared root, exercise and child directories without following an entry through a trailing slash:
ls -ld ~/linux-practice
ls -ld ~/linux-practice/locate-files-by-criteria
ls -ld ~/linux-practice/locate-files-by-criteria/reports
ls -ld ~/linux-practice/locate-files-by-criteria/reports/drafts
ls -ld ~/linux-practice/locate-files-by-criteria/notes
ls -ld ~/linux-practice/locate-files-by-criteria/archive.txt
Require the recognized ordinary owned directories. Enter the exercise and verify its location separately:
cd ~/linux-practice/locate-files-by-criteria
pwd
Require the exact recorded exercise path. Inspect all directory inventories and the seven explicit file targets:
ls -a
ls -a reports
ls -a reports/drafts
ls -a notes
ls -a archive.txt
ls -ld -- report.txt reports/report.txt reports/drafts/report.txt reports/guide.md reports/drafts/checklist.md notes/guide.md notes/.hidden.txt
Require only the known fixture, including the hidden note. Every file must remain regular, owned by the recorded user and single-link. Recheck contents if identity is uncertain. Leave unrecognized entries untouched; search output is not a deletion instruction.
The shown y/n answers assume English confirmations. Establish the actual
program's documented responses before removal, and stop if the language or
response convention is unclear. Only after all target checks:
rm -i -- report.txt reports/report.txt reports/drafts/report.txt reports/guide.md reports/drafts/checklist.md notes/guide.md notes/.hidden.txt
Approve only the expected files. To stop approval, decline the current and every remaining prompt; one refusal does not cancel later prompts.
Inspect the deepest child and the two other leaf directories for emptiness:
ls -a reports/drafts
ls -a notes
ls -a archive.txt
Require only . and .. in each. Then remove the deepest named empty child:
rmdir -- reports/drafts
ls -a reports
Require reports now empty too. Only after these emptiness checks, remove the
three known empty children:
rmdir -- reports notes archive.txt
ls -a
Require only the exercise root's dot entries. Return to the shared root and verify it before the final directory removal:
cd ~/linux-practice
pwd
Require the recorded shared-root path, then inspect the exercise entry:
ls -ld -- locate-files-by-criteria
Only for that recognized owned empty directory:
rmdir -- locate-files-by-criteria
ls -a
Retain the shared practice root and other lessons. The searches themselves performed no file mutations, and no shell setting needs restoring.
Resume or rebuild the known tree
After interruption, recheck the ordinary account, command origins, option baseline, parent/child identities and exact current location. Inspect all existing contents and inventories, including hidden names. Repeat read-only searches only once the known fixture state is established.
Do not overwrite an existing source while rerunning the starter block. For a recognized partial tree, create only the verified missing named resources and check them against the supplied mapping. Preserve unknown entries. For a complete restart, remove only the recognized files and emptied directories actually created, using the same named interactive and location guards, then rebuild a fresh empty exercise tree.
The search mechanism is never the cleanup mechanism in this lesson. Keep removal targets explicit rather than turning a printed match set into an unreviewed operation.
Sources and other study views
- S18 — Bash 5.2 manual: quoting, filename expansion and status parameters
- S19 — GNU Coreutils manual: owned fixture creation and file inspection
- S22 — Ubuntu Noble find manual: starting paths, name/type tests and print
The course targets GNU findutils 4.9 on the selected Ubuntu guest, with Bash 5.2 and Coreutils 9.4. The upstream Coreutils reference can describe newer releases; only the bounded taught forms belong to this exercise.
Revision reconstructs the search decisions; Quick Reference retrieves their syntax after study.