Linux System Administration for Beginners

Essential Commands

Search file using Grep

Searching for text patterns across files and directories is a common task on Linux systems. The grep command lets you quickly locate lines that match a specified string or regular expression.

Table of Common Options

OptionDescriptionExample
-iIgnore case (case-insensitive search)grep -i 'error' logfile.txt
-rRecursive search in all subdirectoriesgrep -r 'TODO' /home/user/projects
-vInvert match (show non-matching lines)grep -v 'DEBUG' app.log
-wMatch whole words onlygrep -w 'user' /etc/passwd
-oShow only the part of a line that matchesgrep -o '[0-9]\+' data.txt

General Syntax

grep [OPTIONS] PATTERN [FILE...]
  • PATTERN
    The text or regular expression to search for.
  • FILE
    One or more files or directories to scan. If no file is given, grep reads from standard input.

Note

Always quote patterns that contain spaces or shell metacharacters:

grep -i 'error message' /var/log/syslog

By default, grep performs a case-sensitive search. To find lines containing CentOS in /etc/os-release:

$ grep 'CentOS' /etc/os-release
NAME="CentOS Stream 8"
PRETTY_NAME="CentOS Stream 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"

Use -i to ignore case differences. This matches “centos”, “CentOS”, or “CENTOS” alike:

$ grep -i 'centos' /etc/os-release
NAME="CentOS Stream 8"
ID="centos"
PRETTY_NAME="CentOS Stream 8"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"

3. Recursive Search in Directories

To search through all files in a directory and its subdirectories, combine -r with your pattern:

$ grep -r 'CentOS' /etc/
/etc/centos-release:CentOS Stream release 8
/etc/yum.repos.d/CentOS-Stream-AppStream.repo:# CentOS-Stream-AppStream.repo

Combine -r and -i for a recursive, case-insensitive search:

$ grep -ir 'centos' /etc/
/etc/centos-release:CentOS Stream release 8
/etc/krb5.conf.d/kcm_default_ccache:# On Fedora/RHEL/CentOS, this is /etc/krb5.conf.d/
…

Warning

You may encounter “Permission denied” messages when scanning protected directories. Use sudo if you need elevated privileges:

sudo grep -ir 'centos' /etc/

4. Invert Match

Show only the lines that do not contain the pattern. Use the -v option:

$ grep -vi 'centos' /etc/os-release
VERSION="8"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
ANSI_COLOR="0;31"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"

5. Match Whole Words Only

To avoid matching substrings (e.g., “redhat” when you want “red”), add the -w flag:

$ echo -e "red Red redhat RED" | grep -iw 'red'
red
Red
RED

6. Show Only the Matched Text

By default, grep prints the entire line containing the match. Use -o to display only the portion that matches:

$ grep -oi 'centos' /etc/os-release
CentOS
centos
CENTOS
centos

You’ve now covered the essentials of grep for searching text in files. In upcoming lessons, we will explore more advanced text-processing tools like awk and sed.

Watch Video

Watch video content

Previous
Demo Pagers and VI