Combining searches
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.
Before you start
Section titled “Before you start”Finish Regex basics (B10.3). Keep your B04 lab directory. Prepare only ~/lab/b10/workflow:
mkdir -p ~/lab/b10/workflow/logs/archivecd ~/lab/b10/workflowprintf 'run 1: ready\nrun 2: failed\nrun 3: complete\n' > logs/auth.logprintf 'run 1: ready\nrun 2: failed\n' > logs/archive/old.logThese 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.
Choose the question before the command
Section titled “Choose the question before the command”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 |
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.
Locate a name, then inspect its contents
Section titled “Locate a name, then inspect its contents”Use the name supplied in the first request:
find logs -type f -name 'auth.log'logs/auth.logThis 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:
grep -n 'failed' logs/auth.log2:run 2: failedThe 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.
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$ Steps (6)
- Type
find logs -type f -name 'auth.log' - Press Enter
- Output:
logs/auth.log - Type
grep -n 'failed' logs/auth.log - Press Enter
- 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.
Search text when the filename is unknown
Section titled “Search text when the filename is unknown”For the second request, you have the phrase failed and a known boundary, logs, but no filename. Search that small subtree directly:
grep -rn 'failed' logslogs/auth.log:2:run 2: failedlogs/archive/old.log:2:run 2: failedThe 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.
Quote the selection and save the result
Section titled “Quote the selection and save the result”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:
grep -n '^run [0-9]: failed$' 'logs/auth.log'2:run 2: failedThe 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?
grep -n '^run [0-9]: failed$' 'logs/auth.log' > matches.txtcat matches.txtRun it, then compare with your guess
2:run 2: failedNo. 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.
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$ Steps (8)
- Type
grep -n '^run [0-9]: failed$' 'logs/auth.log' - Press Enter
- Output:
2:run 2: failed - Type
grep -n '^run [0-9]: failed$' 'logs/auth.log' > matches.txt - Press Enter
- Type
cat matches.txt - Press Enter
- 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.
Mistakes to recognize
Section titled “Mistakes to recognize”Piping path text and expecting file contents. Predict what this pipeline tests:
find logs -type f | grep 'failed'echo "$?"Run it, then compare with your guess
1The 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.
grep -n 'failed' logs/missing.loggrep: logs/missing.log: No such file or directoryThe 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.
Try it yourself
Section titled “Try it yourself”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
find logs/archive -type f -name 'old.log'logs/archive/old.loggrep -n '^run [0-9]: ready$' 'logs/archive/old.log'1:run 1: readyThe first result is a path; the second is a numbered line from that file. Each command answers a different part of the request.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b10ls -AR workflowrm -r ~/lab/b10/workflowRerun 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
findcarries 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.
Further reading
Section titled “Further reading”- On your system:
man find,man grep, andman bash, sections Pipelines and Redirection. - GNU Findutils: Finding Files.
- GNU Grep: Manual.
- GNU Bash: Pipelines and Redirections.