Skip to content

Searching inside files

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 log has a name you recognize, but reading every entry makes it hard to see the failed logins. Some entries use different capitalization. You need the relevant lines and enough location information to return to them.

By the end, you can select lines by their contents, ignore case deliberately, print source line numbers, and search a small practice directory recursively.

Finish Searching for files (B10.1). Keep the B04 lab directory. Use only ~/lab/b10/text for these synthetic logs:

Terminal window
mkdir -p ~/lab/b10/text/logs/archive
cd ~/lab/b10/text
printf 'Accepted login for learner\nFailed login for guest\nfailed login for trainee\nReview complete\n' > logs/auth.log
printf 'Service ready\nFAILED login for visitor\n' > logs/archive/old.log

These are fixed teaching examples, not records from a real authentication service. Real log wording varies. No administrative access is needed to read them. Use Reset this lesson before repeating the setup.

Read the short source once so you can judge the search:

Terminal window
cat logs/auth.log
Captured output
Accepted login for learner
Failed login for guest
failed login for trainee
Review complete

find from B10.1 tests filesystem entries. To test text inside a file, use grep:

Terminal window
grep 'Failed login' logs/auth.log
Captured output
Failed login for guest

The first argument after grep is the pattern; the next names the input file. The command prints each line containing a match, including the rest of that line. It leaves the source file unchanged. The username appears because it belongs to the selected line, although it is not part of the pattern.

Single quotes keep the space inside one pattern argument. Without those quotes, Bash would give the command separate words. grep would treat a later word as an input filename, rather than as the second half of your search text.

By default, grep interprets its pattern as a regular expression, a text-matching language. This phrase contains no regex special characters, so it matches the phrase literally. B10.3 explains the special characters before you use them in a more selective pattern.

Predict first: will the preceding command also print the line beginning with lowercase failed?

Run it, then compare with your guess

No. The default search is case-sensitive. The uppercase F in the pattern does not match the lowercase f in that line. An empty or partial result can therefore reflect the pattern, rather than missing data.

Use -i to ignore case when matching:

Terminal window
grep -i 'failed login' logs/auth.log
Captured output
Failed login for guest
failed login for trainee

Both failed-login lines are now selected. The output retains their original capitalization; -i changes the comparison, not the file or its displayed text. Decide based on your question: ignoring case helps here because the synthetic fixture uses several spellings for the same event description.

Add -n when you need to return to a line:

Terminal window
grep -in 'failed login' logs/auth.log
Captured output
2:Failed login for guest
3:failed login for trainee

The short options can be combined: -in means -i and -n. Each prefix is the line’s number in the input file, starting at 1, followed by a colon. The second selected line still has number 3, even though it is only the second line of this command’s output.

Preserve source line numbers A case-insensitive failed-login pattern selects lines 2 and 3 from a four-line file. The number prefixes describe the source positions. Four source lines in auth.log Pattern: failed login (-i) Selected: source lines 2 and 3 -n prints 2: and 3: prefixes
Preserve source line numbers A case-insensitive failed-login pattern selects lines 2 and 3 from a four-line file. The number prefixes describe the source positions. Four source lines in auth.log Pattern: failed login (-i) Selected: source lines 2 and 3 -n prints 2: and 3: prefixes
grep tests the source lines, then prints matching lines with their original numbers when -n is used.

In words: the file supplies four lines, the pattern selects source lines 2 and 3, and -n preserves those source positions. It does not renumber the selection from 1. A line number is a location in this version of the file; editing earlier lines can change it.

Predict first: does a search naming only logs/auth.log automatically inspect the archived log too?

Run it, then compare with your guess

No. A named file supplies only that file’s text. Add recursive search and name the practice directory to inspect the subtree.

Terminal window
grep -rin 'failed login' logs
Captured output
logs/auth.log:2:Failed login for guest
logs/auth.log:3:failed login for trainee
logs/archive/old.log:2:FAILED login for visitor

Here -r means recursive: descend through directories below logs and search their files. The result includes the archived entry as well. Each selected line now has a filename prefix and its source line number. The final portion is the original log line.

Find failed-login lines and their locations
learner@rootvolt:~/lab$ cd ~/lab/b10/textlearner@rootvolt:~/lab/b10/text$ grep -in 'failed login' logs/auth.log2:Failed login for guest
3:failed login for traineelearner@rootvolt:~/lab/b10/text$ grep -rin 'failed login' logslogs/auth.log:2:Failed login for guest
logs/auth.log:3:failed login for trainee
logs/archive/old.log:2:FAILED login for visitorlearner@rootvolt:~/lab/b10/text$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Case-insensitive matching selects both spellings in auth.log; bounded recursion also finds the archived failed-login entry.
Steps (8)
  1. Type cd ~/lab/b10/text
  2. Press Enter
  3. Type grep -in 'failed login' logs/auth.log
  4. Press Enter
  5. Output: 2:Failed login for guest 3:failed login for trainee
  6. Type grep -rin 'failed login' logs
  7. Press Enter
  8. Output: logs/auth.log:2:Failed login for guest logs/auth.log:3:failed login for trainee logs/archive/old.log:2:FAILED login for visitor

Read a result from left to right: source path, line number, then contents. The colons separate these fields for this controlled fixture. A general program handling arbitrary filenames needs more careful delimiters; do not build automation by splitting this display on every colon.

All recursive searches here name logs, inside the practice directory. Do not replace that argument with / or your home directory. A smaller boundary also makes it easier to understand which files contributed results. GNU grep -r does not follow symbolic links discovered during recursion; the fixture contains only directories and regular text files.

Confusing no match with an unreadable file. First search for text absent from the valid input:

Terminal window
grep 'absent' logs/auth.log
echo "$?"
Captured output
1

The search prints no lines. Its status is 1, meaning no line was selected. That is an expected search answer, not an input error.

Now name an input that does not exist:

Terminal window
grep 'failed login' logs/missing.log
Captured output
grep: logs/missing.log: No such file or directory
Terminal window
echo "$?"
Captured output
2

This invocation returns 2 and reports the missing file. GNU grep uses 0 for a selected line, 1 for no selected lines, and normally 2 for an error. Read diagnostics as well as the status, following B09.4. Searching several files can produce matching lines and still report an error for another input.

Expecting only the matching words. The default output is the whole matching line. A username or other adjacent text can appear without being part of the match.

Using find to read the logs. A name test can locate auth.log; it cannot tell you whether a failed-login phrase occurs inside it. Pick the tool according to what you know about the target.

Searching authentication logs is a useful way to locate evidence for inspection. A failed-login line alone does not establish an intrusion: it records an unsuccessful attempt according to that log’s format. Preserve the source and context before drawing a conclusion. This fixture teaches line selection, without claiming to model a complete security investigation.

Goal: Find the accepted-login entry in logs/auth.log without depending on its capitalization. Include its original line number. Do not search the archive or alter either log.

Hint

Use one named file and combine the two display and matching options taught above.

Solution
Terminal window
grep -in 'accepted login' logs/auth.log
Captured output
1:Accepted login for learner

The prefix identifies source line 1. No recursion is needed when the question names a particular file.

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

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

  • grep selects lines from file contents; its ordinary output includes the whole selected line.
  • -i ignores case during matching; -n adds original source line numbers.
  • -r searches below a named directory. Keep that directory inside the practice fixture.
  • No match and an input error have different meanings and statuses.

Next, Regex basics makes the pattern more precise.

  • On your system: man grep, especially General Output Control, File and Directory Selection, and Exit Status.
  • GNU Grep: Manual, including matching control and recursive searches.
  • Ubuntu 24.04: grep(1).