| |

LFCS 16 ๐Ÿง Text Processing โ€” grep Basics

grep is the command-line equivalent of the find function in a text editor: it searches input for lines that match a pattern and prints the matching lines to standard output. It is one of the most frequently used commands in Linux administration, and the LFCS exam tests it as part of the “Search files and content” objective . The name comes from the ed editor’s command g/re/p โ€” “globally search a regular expression and print” โ€” and that origin explains the default behavior: it works line by line, and it prints the whole line, not just the matched fragment .

The command is line-oriented. It reads input from files or from standard input, processes one line at a time, and selects the lines that match. A line either matches or it does not; there is no partial selection at the default output. This makes grep the natural tool for filtering logs, finding configuration entries, and extracting records from structured text. It is also the first stage in a pipeline: grep finds the lines, and other tools like cut, sort, and awk process them further .

This chapter covers three areas. First, why grep exists and how it fits into text processing โ€” the problem of finding lines in large files and the pipeline model. Second, how the basic command and its core options work โ€” the pattern argument, the file arguments, and the options for case, inversion, counting, and line numbers. Third, how to search multiple files and directories โ€” the filename prefix behavior, recursive search, and the exit status that makes grep useful in scripts. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the grep pipeline.

Key point: grep searches input for lines matching a pattern and prints matching lines. The basic syntax is grep [options] pattern [file...]. If no files are given, it reads standard input. Exit status is 0 if any lines matched, 1 if none matched, and 2 if an error occurred .


Why grep exists

The line-filtering problem. Text files contain many lines, and most tasks require finding a subset. A log file has thousands of entries; the administrator needs the errors. A configuration file has hundreds of settings; the administrator needs the one that controls a specific parameter. grep solves this by reading the file line by line and printing only the lines that match a pattern. The pattern can be a literal string or a regular expression, which gives it the power to describe complex conditions .

The pipeline problem. grep is designed to be one stage in a pipeline. Its output is lines, and its input is lines, so it can be composed with other line-oriented tools. grep finds the relevant lines, cut extracts fields, sort orders them, and uniq removes duplicates. The LFCS exam scenarios reflect this: extracting usernames from /etc/passwd that use a specific shell is a grep | cut | sort pipeline . The command is not a complete solution; it is a filter that fits into a larger workflow.

The exit-status problem. grep is not just an interactive tool. Its exit status makes it useful in scripts. A return code of 0 means at least one line matched. A return code of 1 means no lines matched. A return code of 2 means an error occurred, such as a file that could not be read . A script can test the return code with if grep -q ...; then to make a decision based on whether a pattern exists. The -q option suppresses output and exits immediately on the first match, which is the efficient way to test for existence .

The case-sensitivity problem. By default, grep is case-sensitive. A search for error does not match Error or ERROR. This is correct for precise searches, but many practical searches need to be case-insensitive. The -i option makes the match case-insensitive, which is useful for log files and user input where the case is not consistent .

The inversion problem. Sometimes the goal is to find lines that do not match a pattern. A configuration file might need to be checked for the absence of a setting, or a list of users might need to exclude a specific account. The -v option inverts the match, selecting lines that do not match the pattern . This is the complement of the default behavior, and it is useful in pipelines where the exclusion is easier to describe than the inclusion.

The trade-off. grep is a filter, not a query language. It does not understand the structure of the files it searches; it only sees lines. A search for a pattern that appears in multiple contexts will return all of them, and the administrator must read the output to determine which are relevant. The pattern language (regular expressions) is powerful but can be complex, and a poorly written pattern can match more or less than intended. The trade-off is between the simplicity of a line-oriented filter and the precision of a structured query. For the majority of text-processing tasks, the simplicity is an advantage.


a. The basic command

The basic invocation is grep pattern file. The pattern is the first non-option argument, and the remaining arguments are the files to search .

grep "error" /var/log/syslog

This searches /var/log/syslog for lines containing the string error and prints each matching line. The pattern is quoted to prevent the shell from interpreting special characters. If the pattern contains spaces or shell metacharacters, quoting is required .

If no files are specified, grep reads from standard input:

cat /var/log/syslog | grep "error"

This is equivalent to grep "error" /var/log/syslog, but it demonstrates the filter model. grep reads from the pipe, processes the lines, and writes matching lines to standard output.

The pattern is treated as a basic regular expression (BRE) by default. In BRE, the metacharacters are ^ (start of line), $ (end of line), . (any character), * (zero or more of the previous), [...] (character class), and \ (escape) . A literal search for a string that contains these characters requires escaping them. The -F option disables regular-expression interpretation entirely and treats the pattern as a fixed string, which is faster when no regex features are needed .


b. Core options for output control

The options fall into a few categories: matching control determines which lines are selected, and output control determines what is printed.

Matching control:

  • -i ignores case. grep -i "error" file matches error, Error, and ERROR .
  • -v inverts the match. grep -v "error" file prints lines that do not contain error .
  • -w matches whole words only. grep -w "cat" file matches cat but not concatenate .
  • -x matches whole lines only. grep -x "exact line" file matches only lines that are exactly exact line .

Output control:

  • -c prints only a count of matching lines. grep -c "error" file prints the number of lines containing error .
  • -n prefixes each matching line with its line number. grep -n "error" file prints 3:error message .
  • -l prints only the names of files that contain matches. grep -l "error" *.log prints the filenames, not the matching lines .
  • -o prints only the matched part of the line, not the whole line. grep -o "error" file prints error for each match .
  • -q suppresses all output and exits with status 0 if any match is found. This is the option for scripting .
  • -s suppresses error messages about nonexistent or unreadable files .

Context control:

  • -A n prints n lines of context after each match.
  • -B n prints n lines of context before each match.
  • -C n prints n lines of context before and after .

Context control is useful for understanding the surrounding code or log entry, not just the matching line itself.


c. Searching multiple files and directories

When multiple files are given, grep prefixes each matching line with the filename:

grep "error" /var/log/syslog /var/log/auth.log

The output is:

/var/log/syslog:error message
/var/log/auth.log:authentication error

The filename prefix is automatic when more than one file is searched. To force the prefix when searching a single file, add /dev/null as a second file argument:

grep "error" /var/log/syslog /dev/null

This is a common trick to get consistent output format regardless of the number of files .

For recursive search, the -r option searches all files in a directory and its subdirectories:

grep -r "error" /var/log

The -R option does the same but follows symbolic links. The --include and --exclude options filter which files are searched:

grep -r --include='*.conf' "error" /etc

This searches only files ending in .conf .

The -r option is useful but can be slow on large directory trees. For more control over which files are searched, the recommended pattern is to use find to generate the file list and pipe it to grep:

find /etc -name '*.conf' -exec grep -H "error" {} +

The -H option forces the filename prefix, and -exec ... + batches multiple files into a single grep invocation for efficiency .


Complete Example Session

# ============================================
# PART 1: BASIC SEARCH
# ============================================

# Search a file for a literal string
grep "error" /var/log/syslog

# Search multiple files (filename prefix automatic)
grep "error" /var/log/syslog /var/log/auth.log


# ============================================
# PART 2: CASE-INSENSITIVE SEARCH
# ============================================

grep -i "error" /var/log/syslog
# Matches: error, Error, ERROR


# ============================================
# PART 3: INVERTED MATCH
# ============================================

grep -v "error" /var/log/syslog
# Prints lines that do NOT contain "error"


# ============================================
# PART 4: COUNT AND LINE NUMBERS
# ============================================

grep -c "error" /var/log/syslog
# Output: 42 (number of matching lines)

grep -n "error" /var/log/syslog
# Output: 15:error message
#         42:another error


# ============================================
# PART 5: WHOLE WORD AND WHOLE LINE
# ============================================

grep -w "cat" animals.txt
# Matches "cat" but not "concatenate"

grep -x "exact line" file.txt
# Matches only lines that are exactly "exact line"


# ============================================
# PART 6: RECURSIVE SEARCH
# ============================================

grep -r "error" /var/log
# Searches all files under /var/log

grep -r --include='*.conf' "error" /etc
# Searches only *.conf files


# ============================================
# PART 7: FILE NAMES ONLY
# ============================================

grep -l "error" *.log
# Output: app.log
#         system.log


# ============================================
# PART 8: QUIET MODE FOR SCRIPTING
# ============================================

if grep -q "error" /var/log/syslog; then
  echo "Errors found"
else
  echo "No errors"
fi

# -q exits immediately on first match


# ============================================
# PART 9: CONTEXT LINES
# ============================================

grep -C 2 "panic" /var/log/kern.log
# Prints 2 lines before and after each match


# ============================================
# PART 10: THE LFCS PIPELINE
# ============================================

# Extract usernames from /etc/passwd that use /bin/bash
grep "/bin/bash" /etc/passwd | cut -d: -f1 | sort > /root/bash-users.txt

# Verify
cat /root/bash-users.txt

The ten parts show basic search, case-insensitive search, inverted match, count and line numbers, whole word and whole line, recursive search, file names only, quiet mode for scripting, context lines, and the LFCS pipeline scenario .


Quick Reference

Basic Syntax

FormMeaning
grep pattern fileSearch file for pattern
grep pattern file1 file2Search multiple files
command | grep patternSearch standard input
grep -e pattern filePattern starts with -

Matching Control

OptionEffect
-iIgnore case
-vInvert match (non-matching lines)
-wMatch whole words only
-xMatch whole lines only
-FTreat pattern as fixed string

Output Control

OptionEffect
-cCount matching lines
-nPrefix with line number
-lPrint file names only
-oPrint only matched part
-qQuiet; exit status only
-sSuppress error messages

Context Control

OptionEffect
-A nn lines after match
-B nn lines before match
-C nn lines before and after

Exit Status

StatusMeaning
0At least one line matched
1No lines matched
2Error occurred

Best Practices

โœ… Do This:

# Quote the pattern
grep "error message" file.log                                              # โœ…
# Use -q for existence tests in scripts
if grep -q "pattern" file; then ...                                        # โœ…
# Use -F for literal strings with special chars
grep -F "a.b.c" file.txt                                                   # โœ…
# Use find + grep for complex file selection
find /etc -name '*.conf' -exec grep -H "pattern" {} +                      # โœ…
# Use /dev/null to force filename prefix
grep "pattern" file.txt /dev/null                                          # โœ…

โŒ Don’t Do This:

# Don't forget to quote patterns with spaces
grep error message file.log  # searches for "error" in "message" and "file.log" # โŒ
# Don't parse grep output when exit status suffices
grep -q "pattern" file && echo "found"  # not: grep "pattern" file | wc -l  # โœ…
# Don't use grep for structured queries
# Use awk or cut for field extraction                                   # โœ…
# Don't use -r on / without exclusions
grep -r "pattern" /  # slow and noisy                                      # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Pattern with special chars matches wrongRegex interpretationUse -F or escape
grep returns 1 in scriptNo match foundHandle exit status
Filename prefix missingSingle file searchedAdd /dev/null
Recursive search too slowLarge directory treeUse find + grep
Binary file output garbledBinary file matchedUse -I to skip binary
Case-sensitive missPattern case differsUse -i

Real-World Examples

1. Search a Log File

grep "error" /var/log/syslog

2. Case-Insensitive Search

grep -i "error" /var/log/syslog

3. Count Matches

grep -c "error" /var/log/syslog

4. Invert Match

grep -v "error" /var/log/syslog

5. Show Line Numbers

grep -n "error" /var/log/syslog

6. Recursive Search with Filter

grep -r --include='*.conf' "error" /etc

7. File Names Only

grep -l "error" *.log

8. Quiet Mode for Scripts

grep -q "pattern" file && echo "found"

9. Context Lines

grep -C 3 "panic" /var/log/kern.log

10. Extract Usernames

grep "/bin/bash" /etc/passwd | cut -d: -f1 | sort

Visual

The grep Pipeline

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE GREP PIPELINE                                           โ”‚
โ”‚                                                              โ”‚
โ”‚  Input (file or stdin)                                       โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ–ผ                                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  grep pattern                                        โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Reads line by line                                  โ”‚    โ”‚
โ”‚  โ”‚  Tests each line against the pattern                 โ”‚    โ”‚
โ”‚  โ”‚  Prints matching lines to stdout                     โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ–ผ                                                         โ”‚
โ”‚  Output (matching lines)                                     โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ”œโ”€โ”€โ–บ Terminal (interactive)                               โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ””โ”€โ”€โ–บ Pipe to next command (cut, sort, uniq, awk)          โ”‚
โ”‚                                                              โ”‚
โ”‚  Exit status: 0 (match), 1 (no match), 2 (error)             โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Matching Options

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  MATCHING OPTIONS                                            โ”‚
โ”‚                                                              โ”‚
โ”‚  Input lines:                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  error occurred                                      โ”‚    โ”‚
โ”‚  โ”‚  Error occurred                                      โ”‚    โ”‚
โ”‚  โ”‚  no problem                                          โ”‚    โ”‚
โ”‚  โ”‚  concatenate                                         โ”‚    โ”‚
โ”‚  โ”‚  cat                                                 โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  grep "error"      โ†’ error occurred                          โ”‚
โ”‚  grep -i "error"   โ†’ error occurred, Error occurred          โ”‚
โ”‚  grep -v "error"   โ†’ no problem, concatenate, cat            โ”‚
โ”‚  grep -w "cat"     โ†’ cat                                     โ”‚
โ”‚  grep -w "error"   โ†’ error occurred, Error occurred          โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Output Control

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  OUTPUT CONTROL                                              โ”‚
โ”‚                                                              โ”‚
โ”‚  grep -n "error" file.txt                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  3:error occurred                                    โ”‚    โ”‚
โ”‚  โ”‚  15:another error                                    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  grep -c "error" file.txt                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  2                                                   โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  grep -l "error" *.log                                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  app.log                                             โ”‚    โ”‚
โ”‚  โ”‚  system.log                                          โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  grep -o "error" file.txt                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  error                                               โ”‚    โ”‚
โ”‚  โ”‚  error                                               โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Exit Status for Scripting

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  EXIT STATUS FOR SCRIPTING                                   โ”‚
โ”‚                                                              โ”‚
โ”‚  grep "pattern" file                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Match found      โ†’ exit 0                           โ”‚    โ”‚
โ”‚  โ”‚  No match         โ†’ exit 1                           โ”‚    โ”‚
โ”‚  โ”‚  Error (no file)  โ†’ exit 2                           โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  if grep -q "pattern" file; then                             โ”‚
โ”‚    echo "Pattern exists"                                     โ”‚
โ”‚  else                                                        โ”‚
โ”‚    echo "Pattern not found"                                  โ”‚
โ”‚  fi                                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  -q suppresses output and exits immediately on first match.  โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
grepSearch input for lines matching a pattern
Basic syntaxgrep [options] pattern [file...]
Default inputStandard input if no files given
Default patternBasic regular expression (BRE)
-iCase-insensitive
-vInvert match
-cCount matching lines
-nShow line numbers
-lFile names only
-qQuiet; exit status only
-rRecursive search
Exit status0 match, 1 no match, 2 error

Key takeaways:

  • grep is a line-oriented filter. It reads input line by line, tests each line against a pattern, and prints the lines that match. The default output is the whole line, not the matched fragment. This makes it the natural tool for filtering logs, configuration files, and structured text .
  • The pattern is a basic regular expression by default. The metacharacters ^, $, ., *, [...], and \ have special meaning. The -F option disables regex interpretation and treats the pattern as a fixed string. The -E option enables extended regular expressions .
  • The core matching options are -i, -v, -w, and -x. -i ignores case, -v inverts the match, -w matches whole words, and -x matches whole lines. These four options cover the majority of matching-control needs .
  • The core output options are -c, -n, -l, -o, and -q. -c counts, -n numbers lines, -l lists files, -o prints only the match, and -q suppresses output for scripting. The choice depends on what the next stage of the pipeline needs .
  • The exit status makes grep useful in scripts. 0 means a match was found, 1 means no match, and 2 means an error. The -q option is the efficient way to test for existence, because it exits immediately on the first match .
  • Multiple files produce a filename prefix. The prefix is automatic when more than one file is searched. To force it for a single file, add /dev/null as a second file argument. This is a common pattern for consistent output format .
  • Recursive search uses -r. The --include and --exclude options filter which files are searched. For complex file selection, the recommended pattern is find piped to grep, which gives more control over the file list .
  • grep is the first stage in a pipeline. The LFCS exam tests text-processing pipelines, such as extracting usernames from /etc/passwd with grep | cut | sort. The command is not a complete solution; it is a filter that fits into a larger workflow .

Remember: grep is the standard tool for finding lines in text. It reads input line by line, tests each line against a pattern, and prints the lines that match. The pattern can be a literal string or a regular expression, and the options control which lines are selected and what is printed. The exit status makes it useful in scripts, and the pipeline model makes it composable with other text-processing tools. For the LFCS exam, the key options are -i, -v, -c, -n, -l, -q, and -r. The command is simple, but it is the foundation of text processing on Linux, and fluency with it is assumed in every subsequent text-processing topic.



Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!