Skip to content

Practice: Finding Files and Text

Module
Finding Files and Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
Prerequisites
On this page

A review handoff needs an exact uppercase FAIL entry from the current batch note. The practice tree also contains an archive, a CSV file with similar text, a lowercase status, and a status with a suffix. A broad search can locate useful clues, but it cannot decide which entry belongs in the final handoff.

This practice combines B10.1–B10.4. Use the lessons and manuals as needed. Your evidence must establish both the selected source and the precise text saved, while showing that the original was preserved.

Finish Searching for files through Combining searches. Keep your B04 lab directory.

Use only disposable ~/lab/b10/practice. Run this setup once, or use Reset this lesson before repeating it:

Terminal window
mkdir -p ~/lab/b10/practice/current/summary.txt ~/lab/b10/practice/archive
cd ~/lab/b10/practice
printf 'check 1: PASS\ncheck 2: FAIL\ncheck 3: FAIL.detail\ncheck 4: fail\n' > 'current/batch notes.txt'
printf 'check 5: FAIL\n' > archive/old.txt
printf 'check 6: FAIL\n' > current/counts.csv

These are fixed synthetic entries. current/summary.txt is intentionally a directory, despite its suffix. current/batch notes.txt has a space in its name. All other lessons’ practice directories remain outside this task. Leave handoff.txt absent until you save the final selection.

Reach these five goals:

  1. Locate only regular files ending in .txt below current. Your result must exclude the directory with that suffix and the CSV file. Explain which test establishes type and which establishes name.
  2. Search the contents of both current and archive for fail, without depending on case. Include filenames and source line numbers. Identify the result from the CSV file and explain why this broader content search includes it.
  3. In the current text file found in goal 1, select only complete lines consisting of check, a space, one digit, : FAIL, and then the end of the line. Keep the source line number and save the selection to handoff.txt. Exclude the lowercase status and the suffixed status.
  4. In the same source file, separately select the complete FAIL.detail entry using a literal dot. Then demonstrate a whole-line regex accepting FAIL followed by zero or more arbitrary characters. Explain why that broader pattern selects an additional line.
  5. Inspect the saved handoff and prove that the original text file still contains its four original entries. Compare their newline counts. Explain why the saved number prefix is evidence from the search rather than a modification to the source.

Before running goal 3, predict: does adding quotes around FAIL make matching case-insensitive or require a whole-line match?

Run it, then compare with your guess

Neither. Quotes protect the pattern argument from Bash. Case behavior comes from grep’s options, and line boundaries come from regex anchors. This selection needs the default case-sensitive matching and both anchors.

  • The filename search returns the spaced regular-file path inside current, without the directory or CSV file.
  • The broad content search finds uppercase and lowercase text, including the suffixed status. Its results also identify the matching archive and CSV sources.
  • The final handoff contains one selected entry, with original source line number 2. It contains no archived, CSV, lowercase, or suffixed entry.
  • The literal-dot search identifies source line 3. The broader repetition pattern selects source lines 2 and 3.
  • The source still has four entries and four ending newlines. The handoff has one selected entry and one ending newline.

Record which patterns belong to filename globbing and which belong to regex. No search needs /, your whole home directory, or elevated privileges.

Hint for goals 1 and 2

A filename suffix does not establish entry type. Use both tests for goal 1. For goal 2, pass current and archive as the bounded recursive inputs; the question does not restrict their file suffixes.

Hint for goal 3

Use a single digit class between literal text, with both anchors. Preserve the spaced filename as one argument. Save to a different path from the source, using the redirection taught in B09.

Hint for goals 4 and 5

For the literal suffix, escape the regex dot. For any suffix, use the item meaning any character followed by its repetition operator. Read the source again after saving; a successful search status alone does not prove preservation.

Solution

Establish the current source path.

Terminal window
find current -type f -name '*.txt'
Captured output
current/batch notes.txt

-type f excludes current/summary.txt, which is a directory. The quoted filename glob excludes counts.csv. The quotes prevent Bash from expanding the glob, leaving it for find to interpret. Searching current instead of the whole practice root also excludes the archive.

Inspect the broader content clues.

Terminal window
grep -rin 'fail' current archive
Captured output
current/batch notes.txt:2:check 2: FAIL
current/batch notes.txt:3:check 3: FAIL.detail
current/batch notes.txt:4:check 4: fail
current/counts.csv:1:check 6: FAIL
archive/old.txt:1:check 5: FAIL

The source path and line number precede every selected line. Case-insensitive matching includes lowercase fail; substring matching includes FAIL.detail. Recursive search reads the CSV file too because no filename-suffix filter was requested. This is useful discovery evidence, but it is broader than the final handoff’s rule.

Select and save only the exact uppercase status.

Terminal window
grep -n '^check [0-9]: FAIL$' 'current/batch notes.txt' > handoff.txt
cat handoff.txt
Captured output
2:check 2: FAIL

The anchors and class express the required structure. Omitting -i keeps the case-sensitive comparison. The pattern and spaced filename each reach grep as one argument. Bash saves stdout to handoff.txt, a separate destination.

The prefix 2: comes from -n. The rest of the line is the original entry. A content search does not remove the other source lines simply because they were not selected.

Compare a literal suffix with repetition.

Terminal window
grep -n '^check [0-9]: FAIL\.detail$' 'current/batch notes.txt'
Captured output
3:check 3: FAIL.detail

\. requires an actual dot, followed by the literal letters detail. The anchors reject extra text before or after the requested structure.

Terminal window
grep -n '^check [0-9]: FAIL.*$' 'current/batch notes.txt'
Captured output
2:check 2: FAIL
3:check 3: FAIL.detail

Here . is the any-character item, and * repeats it zero or more times. The exact FAIL entry uses zero repetitions; FAIL.detail uses the suffix characters. The lowercase line remains excluded. This is regex repetition inside file contents, not Bash’s filename wildcard expansion.

Prove the source and destination states.

Terminal window
cat -n 'current/batch notes.txt'
Captured output
1 check 1: PASS
2 check 2: FAIL
3 check 3: FAIL.detail
4 check 4: fail

All four entries remain in their original order and capitalization. The displayed number prefixes come from this cat -n inspection, not from text inserted into the file.

Terminal window
wc -l 'current/batch notes.txt' handoff.txt
Captured output
4 current/batch notes.txt
1 handoff.txt
5 total

The counts support the text inspection: four source lines, one saved selected line, five newlines across both inputs. A count alone would not establish which lines are present; the preceding reads provide that evidence.

Save one selected entry and preserve the source
learner@rootvolt:~/lab$ cd ~/lab/b10/practicelearner@rootvolt:~/lab/b10/practice$ grep -n '^check [0-9]: FAIL$' 'current/batch notes.txt' > handoff.txtlearner@rootvolt:~/lab/b10/practice$ cat handoff.txt2:check 2: FAILlearner@rootvolt:~/lab/b10/practice$ cat -n 'current/batch notes.txt'     1	check 1: PASS
     2	check 2: FAIL
     3	check 3: FAIL.detail
     4	check 4: faillearner@rootvolt:~/lab/b10/practice$ 

Contents text of the file

handoff.txt~/lab/b10/practice/
  1. 2:check 2: FAIL

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Saving the exact FAIL selection creates a one-line handoff while the four-line source stays unchanged.
Steps (10)
  1. Type cd ~/lab/b10/practice
  2. Press Enter
  3. Type grep -n '^check [0-9]: FAIL$' 'current/batch notes.txt' > handoff.txt
  4. Press Enter
  5. Type cat handoff.txt
  6. Press Enter
  7. Output: 2:check 2: FAIL
  8. Type cat -n 'current/batch notes.txt'
  9. Press Enter
  10. Output: 1 check 1: PASS 2 check 2: FAIL 3 check 3: FAIL.detail 4 check 4: fail

The Contents panel follows the created handoff; the terminal then reads the source to confirm that it retains all four entries. Both inspections come from the same verified run.

Preserve the source and save the selection The exact uppercase FAIL selection saves one numbered line separately. The four original source lines remain unchanged. Read four original source lines Select the exact uppercase FAIL Save 2:check 2: FAIL separately Source retains all four lines
Preserve the source and save the selection The exact uppercase FAIL selection saves one numbered line separately. The four original source lines remain unchanged. Read four original source lines Select the exact uppercase FAIL Save 2:check 2: FAIL separately Source retains all four lines
The four-line source remains intact while the exact uppercase status produces one numbered handoff line.

In words: the selection reads four source lines and saves only the complete uppercase failed entry, using a separate destination. The source’s four lines and the handoff’s one line have different roles; the selected text does not replace its source.

Answer before opening each explanation.

1. Why does a name ending in .txt not prove that the entry is a regular file?

Answer

A name is separate from an entry’s type. The practice directory summary.txt has that suffix too. Combine a name test with -type f when the question requires regular files.

2. Why can a database search disagree with a successful live search for a newly created file?

Answer

A filename database reflects a previous collection of paths. It may not yet include the new file. A live find traversal inspects the chosen tree now, subject to access and changes during traversal. The module does not require an installed database tool.

3. Which part of a recursive grep result tells you where to return for context?

Answer

The filename and original source line number. The selected text alone may occur in several files. Choose the input boundary to fit the question, then retain those location prefixes when context matters.

4. How do FAIL.* and the shell glob FAIL* differ?

Answer

In regex, . is an any-character item and * repeats it. In a shell filename glob, * itself matches zero or more filename characters after the literal prefix. Bash expands an unquoted glob into names; quoted grep patterns are interpreted inside file contents.

5. Does find current -type f | grep 'FAIL' search the files’ contents?

Answer

No. The pipe carries the paths that find prints. grep tests that path text on stdin. Use a named input file or a bounded recursive grep search to inspect contents in this workflow.

6. Why do quotes around a pattern not make . literal for grep?

Answer

Quotes protect the argument from Bash. The receiving program still applies its own pattern rules. Use \. in a single-quoted regex when you require a literal dot.

Inspect only this practice directory:

Terminal window
cd ~/lab/b10
ls -AR practice
Terminal window
rm -r ~/lab/b10/practice

Rerun Before you start. If the directory is already absent, skip removal. The setup recreates only the input files and directories, leaving handoff.txt absent.

  • File type, filename, and file contents answer separate questions.
  • Broad searches locate clues; quoted whole-line regex can express a precise final selection.
  • Globs and regex interpret * differently, even when their printed patterns look similar.
  • Inspect saved text and the unchanged source to establish what the workflow actually did.

Next, Identity and groups connects your account to group memberships and file ownership.