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

Regular Expression

Regular Expression

Understanding Regular Expression in Linux

Regular Expression is widely used in many programming languages. It is also called regex or regexp. Basically, it defines the pattern of strings with meta characters to effectively identify specific data in a data set.

The grep command uses regex to specify a search string pattern. In this section, we'll explain selected regex meta characters using the grep command. We use the same data set used in the previous section to show some examples.

^ : begin with

^ defines data that begins with a specified string after ^. For example, to search the lines that begin with "I like a" in the data set prepared in the previous section, run the following command.

Command Line - INPUT
grep -r ^"I like a" test

The command returns a line like the one below. The result doesn't include the lines with "I like a" in the middle of the sentence.

Command Line - RESPONSE
test/sample_1.txt:I like apples and he likes oranges

$ : end with

$ defines data that ends with a specified string before $. For example, to search the lines that end with "s bananas" in the data set prepared in the previous section, run the following command.

Command Line - INPUT
grep -r "s bananas"$ test

The command returns a result like the one below. The result doesn't include the lines with "s bananas" in the middle of the sentence.

Command Line - RESPONSE
test/sample_1.txt:I like grapes and he likes bananas
test/sample_1.txt:He likes grapes and she likes bananas

[ ] : matches anything contained

[ ] defines one character that matches anything contained within the bracket. For example,
[A-Z] : any uppercase alphabet character
[a-z] : any lowercase alphabet character
[0-9] : any single digit number

For the data set prepared in the previous section, run the command below to see how [ ] works.

Command Line - INPUT
grep -r [AE]S test

The command returns a result like the one below. The result includes the lines with AS or ES.

Command Line - RESPONSE
test/test_sub/sample_2.txt:I like the GRAPES
test/test_sub/sample_2.txt:I like the BANANAS
test/test_sub/sample_2.txt:He likes the GRAPES
test/test_sub/sample_2.txt:He likes the BANANAS
test/test_sub/sample_2.txt:She likes the GRAPES
test/test_sub/sample_2.txt:She likes the BANANAS

[^ ] : matches anything NOT contained

[^] defines one character that matches anything NOT contained within the bracket.

For example, to search the lines with “es” but excluding “kes”, “ges” and “pes”, run the following command.

Command Line - INPUT
grep -r [^kgp]es test

The result will be like the one below.

Command Line - RESPONSE
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

. (dot) : matches any one character

. (dot) defines any one character. Run the following command to search the lines with a string of five characters that begin with “l” and end with “a”.

Command Line - INPUT
grep -r l...a test

The result will be like the one below.

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

* (asterisk) : matches any character(s) or no occurrence

* (asterisk) defines character(s) specified before * or no occurrence. For example, AP*D means APD, APPD, APPPD, or AD.

To see how * works in the data set prepared, run the following command.

Command Line - INPUT
grep -r AP*L*E test

The command searches for a string that matches all the criteria below

  • begins with A
  • ends with E
  • P, L, or no character between A and E

The command result will be like the one below.

Command Line - RESPONSE
test/test_sub/sample_2.txt:I like the APPLE
test/test_sub/sample_2.txt:I like the GRAPES
test/test_sub/sample_2.txt:He likes the APPLE
test/test_sub/sample_2.txt:He likes the GRAPES
test/test_sub/sample_2.txt:She likes the APPLE
test/test_sub/sample_2.txt:She likes the GRAPES

IdeaNote: Asterisk

The meaning of * (asterisk) is different in a regular expression and a wildcard.

In a wildcard, * means any character(s). The * is not related in any way to the character before it.

In a regular expression, * has a relation with the character before *. For example, A* means A, AA, AAA, ..., or no occurrence.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Deleting Files with rm Command

rm (Remove File)

Building CRUD Structures with Django

Basic CRUD Structure in Django

Styling Forms with Crispy Forms in Django

Crispy Forms

Deleting Files with rm Command

rm (Remove File)

Building CRUD Structures with Django

Basic CRUD Structure in Django

Styling Forms with Crispy Forms in Django

Crispy Forms

Tags:

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