CloudsArk
Commands Linux

grep Command Examples in Linux

Practice useful grep command examples for everyday Linux administration and troubleshooting.

grep Command Examples in Linux

Introduction

These examples show practical ways to use grep on a Linux terminal. Each example is written so you can adapt it for administration or troubleshooting.

Example 1: Basic Usage

grep root /etc/passwd

This is the simplest form of the command and is a good starting point before adding options.

Example 2: Common Admin Task

grep -i error /var/log/messages

This example reflects a common task on RHEL, Rocky Linux, AlmaLinux, or similar systems.

Example 3: Useful Option

grep -R "Listen" /etc/httpd

This option helps narrow the result, change behavior, or handle a more realistic target.

Example 4: Real-World Scenario

grep -n "failed" /var/log/secure

Use this pattern when the task moves beyond a single basic command.

Example 5: Verification

grep --version

Example output:

root:x:0:0:root:/root:/bin/bash

Common Mistakes

  • Not quoting patterns that contain spaces or shell metacharacters.
  • Searching huge directory trees recursively without narrowing the path.
  • Assuming grep understands extended regex features unless you use the right option, such as -E.

Quick Reference

grep root /etc/passwd
grep -i error /var/log/messages
grep -R "Listen" /etc/httpd
grep -n "failed" /var/log/secure
grep --version

Summary

Good grep usage means choosing the right option, keeping the target clear, and verifying the result with output you can explain.