Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 2. Linux Key Commands

grep (Global Regular Expression Print)

grep (Global Regular Expression Print)

How to Use grep Command in Linux

The grep (Global Regular Expression Print) command is a frequently used command to search for a string of characters or a pattern (regular expression) in specified files and returns the lines with the string or pattern. Using the -r (recursive) option, you can search a string or pattern in multiple files under a specified directory. There are many other options to conduct more tailored searches.

To demonstrate the grep command, we use the following directories and files in this section.

Directory structure

.
└── test
    ├── sample_1.txt
    └── test_sub
        └── sample_2.txt
sample_1.txt
I like apples and he likes oranges
I like oranges and he likes grapes
I like grapes and he likes bananas
I like bananas and she likes apples
He likes apples and she likes oranges
He likes oranges and she likes grapes
He likes grapes and she likes bananas
He likes bananas and I like apples
She likes apples and I like oranges
She likes oranges and I like grapes
She likes grapes and I like bananas
She likes bananas and he likes apples
sample_2.txt
I like the APPLE
I like the ORANGE
I like the GRAPES
I like the BANANAS
He likes the APPLE
He likes the ORANGE
He likes the GRAPES
He likes the BANANAS
She likes the APPLE
She likes the ORANGE
She likes the GRAPES
She likes the BANANAS

Basic grep syntax

To run the grep command, you need to specify a search phrase (or pattern) followed by a file path.

For example, to search "apple" in the sample_1.txt file, run the command below.

Command Line - INPUT
grep apple test/sample_1.txt

The command returns the lines that have the string “apple”.

Command Line - RESPONSE
I like apples and he likes oranges
I like bananas and she likes apples
He likes apples and she likes oranges
He likes bananas and I like apples
She likes apples and I like oranges
She likes bananas and he likes apples

Options

There are many useful options for the grep command.

-r (recursive) option

With the -r option, you can search a string or pattern in multiple files under a specified directory.

For example, to search "He" under the test directory, run the command below.

Command Line - INPUT
grep -r He test

The command returns multiple lines with the string "He" from multiple files. The file path is shown before each line like in the example below.

Command Line - RESPONSE
test/test_sub/sample_2.txt:He likes the APPLE
test/test_sub/sample_2.txt:He likes the ORANGE
test/test_sub/sample_2.txt:He likes the GRAPES
test/test_sub/sample_2.txt:He likes the BANANAS
test/sample_1.txt:He likes apples and she likes oranges
test/sample_1.txt:He likes oranges and she likes grapes
test/sample_1.txt:He likes grapes and she likes bananas
test/sample_1.txt:He likes bananas and I like apples

-i option

The grep command differentiates upper and lower case characters; however, with the -i option, you can search a string regardless of upper or lower case.

For example, to search "apple" regardless of upper or lower case, run the command below.

Command Line - INPUT
grep -ir apple test

The command returns the lines with the string "apple" and "APPLE".

Command Line - RESPONSE
test/test_sub/sample_2.txt:I like the APPLE
test/test_sub/sample_2.txt:He likes the APPLE
test/test_sub/sample_2.txt:She likes the APPLE
test/sample_1.txt:I like apples and he likes oranges
test/sample_1.txt:I like bananas and she likes apples
test/sample_1.txt:He likes apples and she likes oranges
test/sample_1.txt:He likes bananas and I like apples
test/sample_1.txt:She likes apples and I like oranges
test/sample_1.txt:She likes bananas and he likes apples

-n option

When you search data in a file with many lines of code, you may want to know the line number of the identified data. The -n option gives you the line number along with search results.

For example, to search "apple" in the sample_1.txt file, with its line number, run the command below.

Command Line - INPUT
grep -n apple test/sample_1.txt

The command returns the lines with the string "apple" along with line numbers.

Command Line - RESPONSE
1:I like apples and he likes oranges
4:I like bananas and she likes apples
5:He likes apples and she likes oranges
8:He likes bananas and I like apples
9:She likes apples and I like oranges
12:She likes bananas and he likes apples

-C option

To understand the contexts of the searched lines of code, you may want to know what the lines of code before and after the identified lines of code are. The -C option enables showing additional lines before and after the identified lines.

For example, to search "APPLE" in the sample_2.txt file, with one line before and after the identified lines with line numbers, run the command below.

Command Line - INPUT
grep -nC 1 APPLE test/test_sub/sample_2.txt

The command returns lines with the string "apple" and one line before and after the identified lines along with their line numbers.

Command Line - RESPONSE
1:I like the APPLE
2-I like the ORANGE
--
4-I like the BANANAS
5:He likes the APPLE
6-He likes the ORANGE
--
8-He likes the BANANAS
9:She likes the APPLE
10-She likes the ORANGE

Instead of using the -C option, you can simply use the number of lines as an option. The command below gives you the same results as the previous command.

Command Line - INPUT
grep -n1 APPLE test/test_sub/sample_2.txt

-v option

The v option is used when you want to show the lines that don't match the specified phrase or pattern.

For example, if you want to identify lines without the character "g", run the command below.

Command Line - INPUT
grep -v g test/sample_1.txt

The command gives you the lines like the ones below.

Command Line - RESPONSE
I like bananas and she likes apples
He likes bananas and I like apples
She likes bananas and he likes apples

-x option

The -x option is used when you want to search the line that is exactly the same as the string specified by you.

For example, if you want to show the lines that are equal to "He likes bananas and I like apples", run the command below.

Command Line - INPUT
grep -x "He likes bananas and I like apples" test/sample_1.txt

The command gives you the line that is exactly the same as the string you specified:

Command Line - RESPONSE
He likes bananas and I like apples

IdeaNote : --color=auto option

The examples in this section show colored output for the grep command. This is because of the --color=auto option. As the OS that we are using for this course (Ubuntu OS on AWS Lightsail) has a preset alias for the grep command with the --color option=auto option, so the outputs of the command are already colored.

Command Line - INPUT
alias
Command Line - RESPONSE
:
alias grep='grep --color=auto'
:

For the alias command, refer to Chapter 6 Alias (Create Command Shortcuts).


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Installing Django: A Simple Guide

Install Django

Undoing and Redoing Actions in Vim Editor

Normal Mode (4) – Undo and Redo

Installing Django: A Simple Guide

Install Django

Undoing and Redoing Actions in Vim Editor

Normal Mode (4) – Undo and Redo

Tags:

Linux Command

Regular Expression

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server