Back to course

54. Searching Text Patterns: The `grep` Command (Basic)

Linux Basics: From Zero to CLI Hero

Finding Needles in Haystacks

grep (Globally search a Regular Expression and Print) is the fundamental tool for searching plain text data, usually receiving its input via a pipe.

Syntax

grep [options] pattern [file...]

Basic Searching

Find all lines in /etc/passwd that contain the username jsmith:

bash $ grep jsmith /etc/passwd jsmith:x:1001:1001:John Smith:/home/jsmith:/bin/bash

Using grep with Pipes

Find all processes currently running that contain 'apache':

bash $ ps aux | grep apache

Ignoring Case (-i)

The -i option makes the search case-insensitive.

bash $ grep -i root /etc/passwd

Matches lines containing 'root' or 'Root' or 'rOoT'