Practice: Finding Files and Text
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.
Before you start
Section titled “Before you start”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:
mkdir -p ~/lab/b10/practice/current/summary.txt ~/lab/b10/practice/archivecd ~/lab/b10/practiceprintf '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.txtprintf 'check 6: FAIL\n' > current/counts.csvThese 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.
Your task
Section titled “Your task”Reach these five goals:
- Locate only regular files ending in
.txtbelowcurrent. Your result must exclude the directory with that suffix and the CSV file. Explain which test establishes type and which establishes name. - Search the contents of both
currentandarchiveforfail, 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. - 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 tohandoff.txt. Exclude the lowercase status and the suffixed status. - In the same source file, separately select the complete
FAIL.detailentry using a literal dot. Then demonstrate a whole-line regex acceptingFAILfollowed by zero or more arbitrary characters. Explain why that broader pattern selects an additional line. - 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.
Expected evidence
Section titled “Expected evidence”- 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
Section titled “Solution”Solution
Establish the current source path.
find current -type f -name '*.txt'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.
grep -rin 'fail' current archivecurrent/batch notes.txt:2:check 2: FAILcurrent/batch notes.txt:3:check 3: FAIL.detailcurrent/batch notes.txt:4:check 4: failcurrent/counts.csv:1:check 6: FAILarchive/old.txt:1:check 5: FAILThe 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.
grep -n '^check [0-9]: FAIL$' 'current/batch notes.txt' > handoff.txtcat handoff.txt2:check 2: FAILThe 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.
grep -n '^check [0-9]: FAIL\.detail$' 'current/batch notes.txt'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.
grep -n '^check [0-9]: FAIL.*$' 'current/batch notes.txt'2:check 2: FAIL3:check 3: FAIL.detailHere . 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.
cat -n 'current/batch notes.txt' 1 check 1: PASS 2 check 2: FAIL 3 check 3: FAIL.detail 4 check 4: failAll 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.
wc -l 'current/batch notes.txt' handoff.txt 4 current/batch notes.txt 1 handoff.txt 5 totalThe 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.
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
2:check 2: FAIL
Steps (10)
- Type
cd ~/lab/b10/practice - Press Enter
- Type
grep -n '^check [0-9]: FAIL$' 'current/batch notes.txt' > handoff.txt - Press Enter
- Type
cat handoff.txt - Press Enter
- Output:
2:check 2: FAIL - Type
cat -n 'current/batch notes.txt' - Press Enter
- 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.
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.
Recall check
Section titled “Recall check”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.
Reset this lesson
Section titled “Reset this lesson”Inspect only this practice directory:
cd ~/lab/b10ls -AR practicerm -r ~/lab/b10/practiceRerun 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.
Further reading
Section titled “Further reading”- On your system:
man find,man grep,man bash,man cat, andman wc. - GNU Findutils: Finding Files.
- GNU Grep: Regular Expressions.
- GNU Bash: Filename Expansion and Redirections.