Searching inside files
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.
Before you start
Section titled “Before you start”Finish Searching for files (B10.1). Keep the B04 lab directory. Use only ~/lab/b10/text for these synthetic logs:
mkdir -p ~/lab/b10/text/logs/archivecd ~/lab/b10/textprintf 'Accepted login for learner\nFailed login for guest\nfailed login for trainee\nReview complete\n' > logs/auth.logprintf 'Service ready\nFAILED login for visitor\n' > logs/archive/old.logThese 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.
Select a line by its contents
Section titled “Select a line by its contents”Read the short source once so you can judge the search:
cat logs/auth.logAccepted login for learnerFailed login for guestfailed login for traineeReview completefind from B10.1 tests filesystem entries. To test text inside a file, use grep:
grep 'Failed login' logs/auth.logFailed login for guestThe 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.
Decide whether capitalization matters
Section titled “Decide whether capitalization matters”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:
grep -i 'failed login' logs/auth.logFailed login for guestfailed login for traineeBoth 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.
Keep the original line numbers
Section titled “Keep the original line numbers”Add -n when you need to return to a line:
grep -in 'failed login' logs/auth.log2:Failed login for guest3:failed login for traineeThe 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.
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.
Search a bounded directory tree
Section titled “Search a bounded directory tree”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.
grep -rin 'failed login' logslogs/auth.log:2:Failed login for guestlogs/auth.log:3:failed login for traineelogs/archive/old.log:2:FAILED login for visitorHere -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.
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$ Steps (8)
- Type
cd ~/lab/b10/text - Press Enter
- Type
grep -in 'failed login' logs/auth.log - Press Enter
- Output:
2:Failed login for guest 3:failed login for trainee - Type
grep -rin 'failed login' logs - Press Enter
- 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.
Mistakes to recognize
Section titled “Mistakes to recognize”Confusing no match with an unreadable file. First search for text absent from the valid input:
grep 'absent' logs/auth.logecho "$?"1The 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:
grep 'failed login' logs/missing.loggrep: logs/missing.log: No such file or directoryecho "$?"2This 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.
Security Angle
Section titled “Security Angle”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.
Try it yourself
Section titled “Try it yourself”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
grep -in 'accepted login' logs/auth.log1:Accepted login for learnerThe prefix identifies source line 1. No recursion is needed when the question names a particular file.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b10ls -AR textrm -r ~/lab/b10/textRerun Before you start. If the directory is already absent, skip removal.
grepselects lines from file contents; its ordinary output includes the whole selected line.-iignores case during matching;-nadds original source line numbers.-rsearches 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.