Linux / Basic: command-line foundations / Lesson 7
Find files and search text safely
Practice scoped file-name and text searches, interpret missing results, and protect diagnostic evidence.
On this page
Lesson goals
- Choose a safe search boundary and distinguish names from contents.
- Interpret search status and review results before sharing.
- Design a bounded search that distinguishes filename selection from content matching.
- Interpret search results and statuses while handling links and filename boundaries.
Define a search boundary
A useful search starts with an explicit directory and a question about its contents. In a disposable lab, choose a small directory that you own and create sample text files with no private data. Record whether you are looking for a file name or for text inside files: those are different operations. A broad search from the filesystem root can produce permission errors, consume time, and expose unrelated names. Starting narrowly makes each result easier to inspect and provides a meaningful baseline when you later expand the scope.
Find names before contents
The find utility walks a directory tree and can select entries by name and type. Quote a wildcard pattern so that the shell passes it to find instead of expanding it in the current directory first. The following example looks for regular files ending in .log beneath an existing lab directory. It only lists paths; it does not delete or modify files. A symbolic link and the file it points to are distinct entries, so check the tool documentation before choosing any link-following behavior.
find ./lab -type f -name '*.log'
This assumes lab exists; quoting passes the pattern intact to find.
Search text as data
The grep utility selects lines that match a pattern. Fixed-string mode is useful when the search term contains punctuation that would otherwise have meaning in a regular expression. In the example, -- separates options from operands and the quoted path is one argument even when it contains spaces. The command assumes the example file exists. A matching line is evidence that the text is present, not proof of an incident or an explanation of its cause; read surrounding context before taking action.
grep -F -- 'timeout' './lab/service log.txt'
The file must exist; -F treats the term as a fixed string and quoting keeps the path as one argument.
Interpret absence and errors
Do not treat every lack of displayed matches as proof that the data is absent. The search may have used the wrong directory, encountered unreadable files, or applied a pattern with unintended meaning. Capture the diagnostic and status immediately and consult the installed grep manual for its documented status values. Distinguish a completed search with no selected lines from an error that prevented a complete search. Adjust the scope or permissions only through the authorized process, then repeat the same test.
Review and protect results
Before sharing results, review file paths and matching lines for personal data, tokens, or other sensitive material. Store a minimal sanitized observation rather than copying an entire log. If a result is surprising, reproduce it on a small synthetic input and compare the exact pattern and locale before changing the search. Keep the original source files untouched during diagnosis. This workflow separates a reliable observation from the later decision about whether any operational change is needed.
Define the tree and the question
A reliable search begins by naming both the starting directory and the property being sought. A relative starting path is interpreted from the current working directory; an absolute one starts at the filesystem root. Confirm the boundary before walking it, especially when similarly named projects exist. Search a disposable tree of synthetic files while learning, and avoid a filesystem-wide walk that can expose unrelated names or encounter inaccessible locations. The result is evidence about the selected tree at that moment, not a permanent inventory.
Name selection and content selection answer different questions. `find` can select entries by type, basename, path, and other metadata; `grep` selects lines from text input. A file named error.log may contain no error text, and a file named notes.txt may contain it. Decide whether the desired output is paths, matching lines, or a count before combining tools. Start with a small fixture whose expected matches and nonmatches are known, and inspect it before expanding the scope.
Read find expressions and preserve patterns
`find` walks the specified starting points and evaluates tests for each encountered entry. `-type f` selects regular files under the default link behavior, while `-type d` selects directories. `-name` compares the basename, not the full path, and shell-style patterns such as `*.log` should be quoted so Bash passes them intact to find. An unquoted pattern might expand against the shell’s current directory before find starts, changing the argument list or causing an error.
Tests next to one another are normally combined as logical AND. Parentheses and OR can express alternatives, but shell metacharacters must themselves be quoted or escaped. Review the expression with a listing action before considering any mutation. GNU find options such as `-maxdepth` and `-mindepth` limit traversal depth relative to the starting point; confirm support in the installed implementation. The examples below use GNU find and only inspect synthetic files. Output order from traversal is not guaranteed, so sort simple lab names when a predictable display is needed.
Match content with explicit semantics
`grep -F` treats a pattern as a fixed string, so a dot is a literal dot rather than a regular-expression wildcard. `-n` prefixes the matching line’s input number, `-i` requests case-insensitive matching, and `-v` selects nonmatching lines. `-x` requires a whole-line match. Quote the pattern to protect it from the shell, and use `--` when a pattern or pathname could begin with a hyphen. Choose the options according to the question rather than assuming a substring match means a whole field matched.
When searching multiple files, grep can display filenames with matching lines or list matching filenames with `-l`. A matching line is not a causal explanation; inspect the relevant surrounding context in the authorized source. Simple line tools do not understand quoted multiline CSV records or structured documents. Use an appropriate parser when those boundaries matter. Locale can affect case handling and ordering, so examples with predictable ASCII ordering select `LC_ALL=C` where needed.
Treat links and unusual names deliberately
By default GNU find does not follow symbolic links during traversal. A link is an entry in its own right, and `-type l` selects links rather than the regular file a link may point to. Following links changes the search boundary and can revisit locations or encounter loops; consult the installed manual and choose that behavior deliberately. A dangling link can still be found as a link even though its target does not exist. Keep the simple lab tree self-contained when comparing these cases.
Filenames can contain spaces, tabs, newlines, and leading hyphens. Quoting protects each shell pathname argument, but a newline-delimited list of names cannot represent every possible filename unambiguously. GNU find’s `-print0` emits NUL-terminated names, and compatible consumers such as `xargs -0` preserve those boundaries. `find ... -exec command {} +` also passes paths as arguments without parsing decorative listing output. Verify exact tool support and use nonmutating consumers in an initial investigation.
Separate no matches from incomplete searches
An empty display is not enough to prove absence. The starting directory may be wrong, a pattern may have different semantics from the intended literal text, or an error may have prevented reading part of the tree. GNU grep normally returns zero when lines are selected, one when none are selected, and two for an error. Capture the status immediately and retain relevant diagnostics separately from selected data. Find’s nonzero status likewise signals a problem that requires investigation; do not infer completeness from a partial list.
Keep the invocation, boundary, identity, pattern, status, and minimal sanitized observation together. Reproduce an unexpected result with small synthetic positive and negative examples before changing the scope. If access is denied, do not broaden permissions or elevate merely to make the search succeed; use the authorized access process. Before sharing, remove sensitive values and unrelated paths from the evidence. Repeat the same bounded query after correcting a proven issue, then compare both results and diagnostics.
20 worked examples
1. Find regular logs
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir -p lab/sub; touch lab/a.log lab/sub/b.log lab/note.txt; find lab -type f -name '*.log' | LC_ALL=C sort
- Find walks lab and tests for regular files whose basename matches *.log.
- Both a.log and sub/b.log satisfy the tests; note.txt does not, and sort orders the two paths.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir -p lab/sub; touch lab/a.log lab/sub/b.log lab/note.txt; find lab -type f -name '*.log' | LC_ALL=C sort
lab/a.log
lab/sub/b.log
The quoted basename pattern selects only regular .log files at both depths.
Why this result follows: Quoted patterns reach find intact, and adjacent find tests are combined as AND.
2. Find directories
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir -p lab/a/sub lab/b; find lab -type d | LC_ALL=C sort
- The lab contains lab, lab/a, lab/a/sub, and lab/b as directories.
- The type d test includes the starting point and all three descendants; sort orders their paths.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir -p lab/a/sub lab/b; find lab -type d | LC_ALL=C sort
lab
lab/a
lab/a/sub
lab/b
The directory test includes the starting directory as well as its descendants.
Why this result follows: Find evaluates its tests for the starting point as well as visited descendants.
3. Limit depth
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir -p lab/a/deep; touch lab/top.log lab/a/nested.log; find lab -maxdepth 1 -type f -name '*.log' | LC_ALL=C sort
- Top.log is one level below lab; nested.log is two levels below.
- Maxdepth 1 includes top.log but prevents traversal to nested.log.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir -p lab/a/deep; touch lab/top.log lab/a/nested.log; find lab -maxdepth 1 -type f -name '*.log' | LC_ALL=C sort
lab/top.log
A maximum depth of one includes immediate entries but excludes nested files.
Why this result follows: GNU find measures depth relative to each starting point. Top.log is one level below lab; nested.log is two levels below.
4. Skip starting point
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir -p lab/a; find lab -mindepth 1 -maxdepth 1 -type d
- Lab is depth zero and its child directory a is depth one.
- Mindepth excludes lab itself, while maxdepth includes a and stops deeper traversal.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir -p lab/a; find lab -mindepth 1 -maxdepth 1 -type d
lab/a
Minimum depth one excludes lab itself while maximum depth one limits traversal.
Why this result follows: GNU find depth bounds restrict which entries are evaluated or visited. Lab is depth zero and its child directory a is depth one.
5. Alternative extensions
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; touch lab/a.log lab/b.txt lab/c.csv; find lab -type f \( -name '*.log' -o -name '*.txt' \) | LC_ALL=C sort
- The type test requires a file and the parenthesized alternatives match .log or .txt.
- A.log and b.txt satisfy both conditions, while c.csv matches neither extension.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; touch lab/a.log lab/b.txt lab/c.csv; find lab -type f \( -name '*.log' -o -name '*.txt' \) | LC_ALL=C sort
lab/a.log
lab/b.txt
Parenthesized OR selects either extension while the file test applies to both.
Why this result follows: Parentheses group the OR alternatives under the common regular-file test. The type test requires a file and the parenthesized alternatives match .log or .txt.
6. Basename matching
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir -p lab/logs; touch lab/logs/readme.txt; find lab -type f -name 'logs'
- The only regular file is lab/logs/readme.txt, whose basename is readme.txt.
- The basename is not logs, so the file test and name test select no path.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir -p lab/logs; touch lab/logs/readme.txt; find lab -type f -name 'logs'
The basename is readme.txt, so the parent directory name does not match -name for the file.
Expected result: No paths are printed.
Why this result follows: Find -name compares the final pathname component rather than its parent directory.
7. Find symbolic links
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; printf x > lab/target; ln -s target lab/link; find lab -type l
- Lab contains a regular target and a symbolic link named link.
- The type l test selects lab/link, not the target regular file.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; printf x > lab/target; ln -s target lab/link; find lab -type l
lab/link
The link entry is selected by type l rather than the regular-file test.
Why this result follows: Under default traversal, find can test the symbolic link as its own entry.
8. Find dangling link
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; ln -s missing lab/broken; find lab -type l
- The link broken points to missing, which was not created.
- The link entry still exists and type l selects lab/broken.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; ln -s missing lab/broken; find lab -type l
lab/broken
The symbolic link exists as an entry even though its target is absent.
Why this result follows: A dangling symbolic link remains a filesystem entry even when its target is absent.
9. Regular file and link
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; printf x > lab/real; ln -s real lab/link; find lab -type f
- Lab contains regular file real and a symbolic link to it.
- With default link handling, type f selects real and does not select link itself.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; printf x > lab/real; ln -s real lab/link; find lab -type f
lab/real
Default traversal does not treat the symbolic link itself as a regular file.
Why this result follows: Find’s type test applies to the link entry under its default non-following behavior.
10. Spaced filename
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; printf x > 'lab/my note.txt'; find lab -type f -name '*.txt' -exec cat -- {} +
- The selected .txt filename contains a space and its content is x.
- Find passes the complete pathname as one argument to cat through -exec, which reads x.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; printf x > 'lab/my note.txt'; find lab -type f -name '*.txt' -exec cat -- {} +
x
Exec passes the complete spaced pathname as one argument to cat.
Why this result follows: Find -exec passes selected pathnames as arguments without splitting their spaces. The selected .txt filename contains a space and its content is x.
11. Fixed punctuation
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'v1.2\nv1X2\n' > data; grep -F 'v1.2' data
- The file contains v1.2 and v1X2; -F makes the pattern literal.
- Only the line with the actual dot contains the fixed string v1.2.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'v1.2\nv1X2\n' > data; grep -F 'v1.2' data
v1.2
Fixed-string mode selects the literal dot and excludes the X variant.
Why this result follows: Fixed-string grep does not interpret the dot as a regex wildcard. The file contains v1.2 and v1X2; -F makes the pattern literal.
12. Whole-line content
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'ready\nalready\n' > data; grep -Fx ready data
- The inputs are ready and already, and -x requires an entire-line match.
- Only ready equals the fixed pattern as a whole line.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'ready\nalready\n' > data; grep -Fx ready data
ready
Whole-line matching excludes the longer word containing the same substring.
Why this result follows: Grep -x excludes longer lines that merely contain the pattern. The inputs are ready and already, and -x requires an entire-line match.
13. Numbered match
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'ok\nwarn\nok\n' > data; grep -n '^warn$' data
- The second input line is warn and the pattern is anchored at both ends.
- Grep selects that line and -n prefixes its original line number 2.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'ok\nwarn\nok\n' > data; grep -n '^warn$' data
2:warn
The number refers to the original input line selected by this grep.
Why this result follows: Line numbering refers to grep’s input, including lines it does not select.
14. Case-insensitive search
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'YES\nno\nyes\n' > data; LC_ALL=C grep -i '^yes$' data
- The C-locale input has YES, no, and yes.
- Case-insensitive anchored matching selects the first and third lines.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'YES\nno\nyes\n' > data; LC_ALL=C grep -i '^yes$' data
YES
yes
In the C locale both ASCII case variants match the anchored pattern.
Why this result follows: Grep -i ignores ASCII case differences under the specified locale. The C-locale input has YES, no, and yes.
15. Inverted search
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'ok\nwarn\nok\n' > data; grep -v '^warn$' data
- The anchored pattern selects the middle warn line.
- The -v option reverses that selection, so both ok lines are printed.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'ok\nwarn\nok\n' > data; grep -v '^warn$' data
ok
ok
Inversion emits the lines not selected by the warning pattern.
Why this result follows: Inverted grep emits lines that do not match the pattern. The anchored pattern selects the middle warn line.
16. Matching filenames
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'red\n' > a; printf 'blue\n' > b; grep -l -F red a b
- File a contains red while file b contains blue.
- Fixed-string grep finds red only in a, and -l prints its filename rather than the line.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'red\n' > a; printf 'blue\n' > b; grep -l -F red a b
a
The -l option reports the filename containing the matching line.
Why this result follows: Grep -l reports files with selected lines. File a contains red while file b contains blue.
17. No selected lines
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf 'oak\npine\n' > data; grep -F elm data; s=$?; printf 'status=%s\n' "$s"
- The file contains oak and pine, neither containing elm.
- Grep selects no line and returns 1; the assignment preserves that status for printing.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf 'oak\npine\n' > data; grep -F elm data; s=$?; printf 'status=%s\n' "$s"
status=1
GNU grep returns one when it reads the input but selects no line.
Why this result follows: GNU grep uses status 1 for no selected lines. The file contains oak and pine, neither containing elm.
18. Missing input error
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 grep -F elm missing 2>err; s=$?; printf 'status=%s\n' "$s"; test -s err; printf 'diagnostic=%s\n' "$?"
- The requested input file missing does not exist; stderr is redirected to err.
- Grep returns 2 and writes a diagnostic; test -s confirms that err is nonempty.
lab=$(mktemp -d)
cd "$lab" || exit 1
grep -F elm missing 2>err; s=$?; printf 'status=%s\n' "$s"; test -s err; printf 'diagnostic=%s\n' "$?"
status=2
diagnostic=0
A missing input produces an error status and a nonempty diagnostic file.
Why this result follows: An input error is distinct from grep’s no-match status. The requested input file missing does not exist; stderr is redirected to err.
19. Leading hyphen pattern
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 printf '%s\n' '-draft' > data; grep -F -- '-draft' data
- The fixed pattern -draft follows the -- option terminator.
- Grep treats it as the pattern and selects the identical line in data.
lab=$(mktemp -d)
cd "$lab" || exit 1
printf '%s\n' '-draft' > data; grep -F -- '-draft' data
-draft
The option terminator keeps the leading-hyphen pattern from being parsed as an option.
Why this result follows: The separator prevents a leading-hyphen operand from being parsed as an option.
20. Search content in selected files
Problem: Trace the lab setup, input, and commands; predict the result and explain it. Input: lab=$(mktemp -d) cd "$lab" || exit 1 mkdir lab; printf 'warn\n' > lab/a.log; printf 'ok\n' > lab/b.log; printf 'warn\n' > lab/c.txt; find lab -type f -name '*.log' -exec grep -l -F warn -- {} +
- Find selects only a.log and b.log, excluding c.txt despite its warn content.
- Grep finds warn in a.log but not b.log, so -l reports only lab/a.log.
lab=$(mktemp -d)
cd "$lab" || exit 1
mkdir lab; printf 'warn\n' > lab/a.log; printf 'ok\n' > lab/b.log; printf 'warn\n' > lab/c.txt; find lab -type f -name '*.log' -exec grep -l -F warn -- {} +
lab/a.log
Find limits the files by name and grep then selects the one containing warn.
Why this result follows: The filename filter bounds the files before grep tests their contents. Find selects only a.log and b.log, excluding c.txt despite its warn content.
Common errors and fixes
Results from the wrong directory
- Why it happens
- A relative path was evaluated from a different working directory.
- How to fix it
- Check the working directory and use the intended lab boundary.
- Prevent it next time
- Record the starting path before searching.
Unexpected wildcard matches
- Why it happens
- The shell expanded the wildcard before find received it.
- How to fix it
- Quote the pattern and repeat the search.
- Prevent it next time
- Review arguments and documentation before using metacharacters.
Treating no results as proof of absence
- Why it happens
- A read error or overly narrow scope was overlooked.
- How to fix it
- Inspect exit status and diagnostics, then repeat the authorized search.
- Prevent it next time
- Keep the search status with its results.
20 practice exercises
1. How would you list regular .log files only beneath lab?
Show hint
Bound the starting tree, select the type, and quote the name pattern.
Show worked solution
Use find ./lab -type f -name '*.log'. The starting path bounds the search, and quoting leaves pattern interpretation to find.
`find ./lab -type f -name '*.log'` selects regular files with matching names in that tree. Quoting preserves the wildcard for find rather than shell expansion.
Check your solution
- Start at ./lab and select -type f.
- Quote the *.log pattern.
2. No match appeared and a permission error was printed. What can you conclude?
Show hint
Distinguish an empty result from a complete search.
Show worked solution
The intended data may not have been searched completely. Preserve the message, check the boundary and identity, and obtain authorized access before repeating the test.
A permission error may have prevented part of the scope from being read, so no displayed match does not establish absence. Preserve the diagnostic and status and check identity before seeking authorized access.
Check your solution
- Identify the possibility of an incomplete search.
- Preserve the diagnostic and status before an authorized repeat.
3. Write a find command for regular `.txt` files beneath the synthetic directory `./notes`, including nested directories.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `find ./notes -type f -name "*.txt"`. The starting path bounds the tree, type f excludes directories, and quoting passes the wildcard to find rather than expanding it in the shell.
Check your solution
- Use `find ./notes -type f -name "*.txt"`.
- The starting path bounds the tree, type f excludes directories, and quoting passes the wildcard to find rather than expanding it in the shell.
4. A project contains `app/error.txt` and `app/archive/error.txt`. Select only immediate regular files beneath `app`. Explain the selection and the expected result in the synthetic tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use GNU `find app -maxdepth 1 -type f`. The immediate error.txt is selected; archive/error.txt is deeper than one level and is excluded from this bounded traversal. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Use GNU `find app -maxdepth 1 -type f`.
- The immediate error.txt is selected; archive/error.txt is deeper than one level and is excluded from this bounded traversal.
5. Explain why quoting `*.log` matters when the shell current directory contains a matching filename. Explain the selection and the expected result in the synthetic tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Without quotes Bash may expand the pattern before find receives it, changing its arguments. Quoting preserves the literal pattern so find evaluates it against each basename in the intended tree.
Check your solution
- Without quotes Bash may expand the pattern before find receives it, changing its arguments.
- Quoting preserves the literal pattern so find evaluates it against each basename in the intended tree.
6. A file `lab/logs/readme.txt` is present. Explain whether `find lab -type f -name logs` selects it.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
It does not select the file: -name tests its basename readme.txt, not the parent directory name logs. Choose a path-oriented test if the question concerns a path component. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- It does not select the file: -name tests its basename readme.txt, not the parent directory name logs.
- Choose a path-oriented test if the question concerns a path component.
7. Select regular `.log` or `.txt` files under `lab` with one parenthesized find expression. Explain the selection and the expected result in the synthetic tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `find lab -type f \( -name "*.log" -o -name "*.txt" \)`. The escaped parentheses group the alternatives, so the regular-file condition applies to either extension. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Use `find lab -type f \( -name "*.log" -o -name "*.txt" \)`.
- The escaped parentheses group the alternatives, so the regular-file condition applies to either extension.
8. A synthetic tree contains a file and a symlink to it. How can find list the link itself?
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `find lab -type l` under GNU find’s default traversal behavior. The link is an entry in its own right; it is distinct from the regular-file target it may reference.
Check your solution
- Use `find lab -type l` under GNU find’s default traversal behavior.
- The link is an entry in its own right; it is distinct from the regular-file target it may reference.
9. Explain why a dangling symbolic link can appear in a link search even when its target is missing.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
The symbolic link itself is a directory entry containing target text. `find -type l` can select that entry without successfully resolving the missing target under default link handling. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- The symbolic link itself is a directory entry containing target text.
- `find -type l` can select that entry without successfully resolving the missing target under default link handling.
10. Search a synthetic file `notes.txt` for the literal text `a.b` without matching `aXb`. Explain the selection and the expected result in the synthetic tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `grep -F -- "a.b" notes.txt`. Fixed-string mode makes the dot literal, and -- ends option parsing; a regular expression without -F would allow the dot to match another character.
Check your solution
- Use `grep -F -- "a.b" notes.txt`.
- Fixed-string mode makes the dot literal, and -- ends option parsing; a regular expression without -F would allow the dot to match another character.
11. Given lines `ok`, `not ok`, and `ok`, select only whole lines equal to `ok`. Explain the selection and the expected result in the synthetic tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `grep -Fx ok` on the fixture. It selects the first and third lines, while -x excludes not ok because the full line is longer than the fixed pattern. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Use `grep -Fx ok` on the fixture.
- It selects the first and third lines, while -x excludes not ok because the full line is longer than the fixed pattern.
12. A file contains `green`, `red`, `green`. Report the input line number of the red match.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `grep -n "^red$" file`. The output is `2:red`, because grep numbers the lines presented to it and the anchored expression selects the second line exactly. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Use `grep -n "^red$" file`.
- The output is `2:red`, because grep numbers the lines presented to it and the anchored expression selects the second line exactly.
13. Find which of synthetic files `a` and `b` contains literal `ready`, without printing matching lines.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `grep -l -F ready a b`. It reports names of files with selected lines rather than their contents; check the status and diagnostics if a file cannot be read.
Check your solution
- Use `grep -l -F ready a b`.
- It reports names of files with selected lines rather than their contents; check the status and diagnostics if a file cannot be read.
14. A grep search reads its input successfully but selects no lines. Interpret GNU grep status 1.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Status 1 means no lines were selected, not necessarily that the command malfunctioned. Check that the boundary and pattern answer the intended question before claiming the data is absent. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Status 1 means no lines were selected, not necessarily that the command malfunctioned.
- Check that the boundary and pattern answer the intended question before claiming the data is absent.
15. No lines appear and grep reports an unreadable input. Explain what conclusion is justified and what to retain.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
The search may be incomplete, so absence is not established. Preserve the status and diagnostic, verify the search boundary and identity, and obtain authorized access before repeating the same query.
Check your solution
- The search may be incomplete, so absence is not established.
- Preserve the status and diagnostic, verify the search boundary and identity, and obtain authorized access before repeating the same query.
16. A filename contains a space. Explain why parsing ordinary newline-formatted `find` output is fragile for general filenames.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Spaces can be preserved with quoting, but filenames may also contain newlines, making newline records ambiguous. Use compatible NUL-delimited interfaces or find -exec to pass paths as distinct arguments. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Spaces can be preserved with quoting, but filenames may also contain newlines, making newline records ambiguous.
- Use compatible NUL-delimited interfaces or find -exec to pass paths as distinct arguments.
17. A literal search pattern begins with `-`. Show how to prevent grep from interpreting it as an option.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Use `grep -F -- "-draft" file` for the synthetic pattern -draft. The -- marker ends option processing, so the next argument is the pattern rather than a switch. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Use `grep -F -- "-draft" file` for the synthetic pattern -draft.
- The -- marker ends option processing, so the next argument is the pattern rather than a switch.
18. Explain why a matching `error` line alone does not establish the cause of an operational incident.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
The line establishes only that the selected text appeared in the searched input. Inspect relevant authorized context, timing, and other evidence before drawing a causal conclusion or changing a system.
Check your solution
- The line establishes only that the selected text appeared in the searched input.
- Inspect relevant authorized context, timing, and other evidence before drawing a causal conclusion or changing a system.
19. Plan a small reproducible investigation when a search returns unexpected results in a larger tree.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Create synthetic positive and negative files, record the exact boundary and quoted pattern, and run the search while capturing status and diagnostics. Compare the output with the known fixture before expanding scope.
Check your solution
- Create synthetic positive and negative files, record the exact boundary and quoted pattern, and run the search while capturing status and diagnostics.
- Compare the output with the known fixture before expanding scope.
20. Before sharing a search result, identify two pieces of evidence to keep and what to remove.
Show hint
Identify the search boundary and matching mode before interpreting the result.
Show worked solution
Keep the bounded query and its status along with the minimal relevant observation. Remove unrelated paths and sensitive values; review surrounding context privately before sharing a sanitized result. Check the stated search boundary, pattern, and status before comparing the result with the expected behavior.
Check your solution
- Keep the bounded query and its status along with the minimal relevant observation.
- Remove unrelated paths and sensitive values; review surrounding context privately before sharing a sanitized result.
Key takeaways
- Start with a small boundary and distinguish names from file contents.
- Interpret status and protect sensitive results.
- Bound the starting tree and quote find patterns.
- No match differs from an input error.
- Use explicit filename boundaries when passing unusual paths.