Skip to content

Combining searches

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 request asks for the failed run in auth.log. You remember the filename, but not its directory. Another request names no file and asks whether any practice log contains a failed run. The two requests need different starting points.

By the end, you can choose a search from the information you have, inspect a returned path, and save selected lines while preserving the source. You can explain what crosses a pipe when a filename search feeds another command.

Finish Regex basics (B10.3). Keep your B04 lab directory. Prepare only ~/lab/b10/workflow:

Terminal window
mkdir -p ~/lab/b10/workflow/logs/archive
cd ~/lab/b10/workflow
printf 'run 1: ready\nrun 2: failed\nrun 3: complete\n' > logs/auth.log
printf 'run 1: ready\nrun 2: failed\n' > logs/archive/old.log

These are synthetic entries, not real operational logs. Both input files remain unchanged throughout the workflow. Use Reset this lesson before repeating the setup, so matches.txt starts absent.

The word “find” in an ordinary request does not necessarily call for the find program. First decide whether your clue describes an entry’s name or text stored inside it.

What you know What to test Command family
A name such as auth.log Filesystem entry names and types find with a starting directory
A phrase such as failed Lines in a file or directory of files grep with an input path
Choose a name search or a text search A name clue leads to find, which returns logs/auth.log. A text clue leads to grep, which returns source line 2: run 2: failed. What do you know? Find a name find logs -type f -name 'auth.log' Result: logs/auth.log Find text grep -n 'failed' logs/auth.log Result: 2:run 2: failed
Choose a name search or a text search A name clue leads to find, which returns logs/auth.log. A text clue leads to grep, which returns source line 2: run 2: failed. What do you know? Find a name find logs -type f -name 'auth.log' Result: logs/auth.log Find text grep -n 'failed' logs/auth.log Result: 2:run 2: failed
A name clue leads to find and logs/auth.log; a text clue leads to grep and the matching source line 2.

In words: for a filename clue, find returns the path logs/auth.log. For a text clue in that known file, grep returns 2:run 2: failed. The diagram’s commands and results come from the same recorded fixture used below. The branches are alternative questions; neither changes a file.

Use the name supplied in the first request:

Terminal window
find logs -type f -name 'auth.log'
Captured output
logs/auth.log

This returns a regular-file path under logs. It establishes where the requested name exists in this practice tree. It says nothing about whether that file contains a failed run.

Use that returned path as the input to a content search:

Terminal window
grep -n 'failed' logs/auth.log
Captured output
2:run 2: failed

The result now identifies both the source line number and its text. No recursive search is needed for this question because the first step supplied a particular file. The find and grep patterns happen to contain ordinary letters, but their languages remain different: filename globbing for -name, regex for grep.

Locate a name, then read matching contents
learner@rootvolt:~/lab/b10/workflow$ find logs -type f -name 'auth.log'logs/auth.loglearner@rootvolt:~/lab/b10/workflow$ grep -n 'failed' logs/auth.log2:run 2: failedlearner@rootvolt:~/lab/b10/workflow$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The name search establishes the file path; the content search establishes which line contains failed.
Steps (6)
  1. Type find logs -type f -name 'auth.log'
  2. Press Enter
  3. Output: logs/auth.log
  4. Type grep -n 'failed' logs/auth.log
  5. Press Enter
  6. Output: 2:run 2: failed

This two-command workflow is intentionally manual. Read the returned path before using it. If a future search returns several plausible files, inspect their paths and choose deliberately rather than assuming the first printed path is the answer.

For the second request, you have the phrase failed and a known boundary, logs, but no filename. Search that small subtree directly:

Terminal window
grep -rn 'failed' logs
Captured output
logs/auth.log:2:run 2: failed
logs/archive/old.log:2:run 2: failed

The output identifies a match in the active log and another in the archived log. B10.2’s filename and line-number prefixes let you return to either source. The search covers only logs below the practice directory, including its archive; it does not inspect / or your whole home directory.

If the request concerns the active file alone, searching the archive adds irrelevant results. Conversely, searching only auth.log cannot answer a question about every practice log. A correct pattern still needs an appropriate input boundary.

The first request needs a full status entry, rather than any line containing the letters failed. Express that structure with the operators from B10.3:

Terminal window
grep -n '^run [0-9]: failed$' 'logs/auth.log'
Captured output
2:run 2: failed

The anchors require the complete line. [0-9] supplies one digit, the colon and spaces are literal, and failed must end the line. Single quotes keep the pattern as one argument and preserve its operators for grep. The quoted path is also one argument; it would remain one if it contained spaces.

Predict first: when you save this selection, will the input log lose its other lines?

Terminal window
grep -n '^run [0-9]: failed$' 'logs/auth.log' > matches.txt
cat matches.txt
Run it, then compare with your guess
Captured output
2:run 2: failed

No. grep reads the input file; Bash redirects its stdout to the separate destination matches.txt. The saved text includes the line-number prefix because -n added it to stdout. That prefix was never added to the source log.

Quote the regex and save the selected lines
learner@rootvolt:~/lab/b10/workflow$ grep -n '^run [0-9]: failed$' 'logs/auth.log'2:run 2: failedlearner@rootvolt:~/lab/b10/workflow$ grep -n '^run [0-9]: failed$' 'logs/auth.log' > matches.txtlearner@rootvolt:~/lab/b10/workflow$ cat matches.txt2:run 2: failedlearner@rootvolt:~/lab/b10/workflow$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. A quoted whole-line regex selects the failed entry and saves the numbered result to a separate file.
Steps (8)
  1. Type grep -n '^run [0-9]: failed$' 'logs/auth.log'
  2. Press Enter
  3. Output: 2:run 2: failed
  4. Type grep -n '^run [0-9]: failed$' 'logs/auth.log' > matches.txt
  5. Press Enter
  6. Type cat matches.txt
  7. Press Enter
  8. Output: 2:run 2: failed

Saving the selected output does not prove that every input was readable. Check diagnostics as well. If the command reports a missing file, correct that path and rerun the search instead of treating an empty saved file as proof of no failures.

Piping path text and expecting file contents. Predict what this pipeline tests:

Terminal window
find logs -type f | grep 'failed'
echo "$?"
Run it, then compare with your guess
Captured output
1

The pipeline prints no selected lines. find writes paths to stdout; the pipe supplies that path text to grep’s stdin. None of these filenames contains failed, so the final command returns 1. It never opens those printed paths to search their contents. Use the named-file or bounded recursive content search above.

This is B09’s stream rule applied to searching: a pipe carries what the left command actually writes. Combining useful commands does not change the meaning of their input automatically. Later work on safe filename handling can automate passing files; this lesson uses inspected paths directly.

Using a missing returned path without checking it.

Terminal window
grep -n 'failed' logs/missing.log
Captured output
grep: logs/missing.log: No such file or directory

The diagnostic identifies the input grep could not open. Confirm the current directory and the exact returned path before changing the regex. A missing input and a valid search with no match are different outcomes.

Goal: Locate the regular file old.log below logs/archive. In that file alone, find a complete ready-status line with one digit after run. Keep its original line number and preserve the file.

Hint

Use the known name to establish a path. Then adapt the whole-line regex to the requested status.

Solution
Terminal window
find logs/archive -type f -name 'old.log'
Captured output
logs/archive/old.log
Terminal window
grep -n '^run [0-9]: ready$' 'logs/archive/old.log'
Captured output
1:run 1: ready

The first result is a path; the second is a numbered line from that file. Each command answers a different part of the request.

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

Rerun Before you start. If the directory is already absent, skip removal.

  • Choose filename search for a name clue and content search for a text clue.
  • Use the smallest practice subtree that answers the question.
  • Inspect returned paths, quote patterns and paths, and save output to a separate destination.
  • A pipe from find carries path text; it does not make grep open the listed files.

Next, Practice: Finding Files and Text builds a precise handoff from several search clues.