Course Outline (Part 3)

Welcome to Part 3 of the Linux Bash course. Text processing is one of the core strengths of the Linux command line. Since “everything is a file” in Linux, operating system configs, logs, and processes are represented as text. In this part, we cover fundamental text tools, master search filtering with grep, and unlock automated find-and-replace using sed.

If you are setting up text environments or containers, check out our guide on Installing Docker on Ubuntu. Feel free to also read the GNU Bash Manual.


Chapter 7: Text Processing Basics

7.1 Concatenating Files: The cat Command

The cat command concatenates and displays file contents on standard output. It can print a single file, merge multiple files together, or generate line numbers.

Syntax:

cat [options] [file...]

Example Command:

cat -n config.txt

Expected Output:

     1	port=8080
     2	debug=true
     3	timeout=30

Flag & Command Breakdown:

  • cat: Concatenate and display utility.
  • -n: Number all output lines, starting from 1.
  • config.txt: The file to read.

7.2 Concatenating in Reverse: The tac Command

The tac command (cat spelled backward) concatenates and displays file contents in reverse order, starting with the last line and ending with the first line.

Syntax:

tac [file...]

Example Command:

tac config.txt

Expected Output:

timeout=30
debug=true
port=8080

Flag & Command Breakdown:

  • tac: Reverses the line ordering of files.

7.3 Interactive File Viewing: The less and more Commands

  • more: An older terminal pager that lets you view files page-by-page. It only allows forward scrolling.
  • less: A modern pager that does not need to read the entire input file before starting, making it extremely fast on huge log files. It supports forward/backward navigation, text searching, and does not litter the terminal screen history after exiting.

Syntax:

less [options] file_name

Example Command:

less -N system.log

Expected Output: (Opens an interactive screen displaying file contents with line numbers on the left margin. Navigation is done via arrow keys, / to search, and q to exit)

Flag & Command Breakdown:

  • less: Interactive pager utility.
  • -N: Displays line numbers on each line.

7.4 Viewing the Top of a File: The head Command

The head command outputs the first part of files (by default, the first 10 lines).

Syntax:

head [options] [file...]

Example Command:

head -n 5 info.txt

Expected Output:

Project Name: TechBlog
Author: Suresh
Created: 2026-06-12
Version: 1.0.0
Description: Bash course tutorials

Flag & Command Breakdown:

  • head: Prints top lines of a file.
  • -n 5: Limits output to the first 5 lines.

7.5 Viewing the Bottom of a File: The tail Command

The tail command outputs the last part of files (by default, the last 10 lines).

Syntax:

tail [options] [file...]

Example Command:

tail -n 3 info.txt

Expected Output:

Version: 1.0.0
Description: Bash course tutorials
Status: Draft

Flag & Command Breakdown:

  • tail: Prints bottom lines of a file.
  • -n 3: Limits output to the last 3 lines.

7.6 Real-time File Monitoring: tail -f

Using the -f (follow) flag makes tail append data as the file grows. This is the industry-standard way to monitor active system and web server logs.

Syntax:

tail -f log_file

Example Command:

tail -f -n 5 access.log

Expected Output:

192.168.1.5 - - [12/Jun/2026:13:30:00] "GET /blog HTTP/1.1" 200 4096
192.168.1.8 - - [12/Jun/2026:13:30:12] "POST /login HTTP/1.1" 302 512
*(Terminal hangs here, printing new hits in real-time until aborted with Ctrl+C)*

Flag & Command Breakdown:

  • tail: Prints bottom lines.
  • -f: Follows the file. It waits for and displays new lines written to the file by other processes.
  • -n 5: Starts by outputting the last 5 existing lines of the file.

7.7 Counting Lines, Words, and Bytes: The wc Command

The wc (Word Count) command counts lines, words, characters, and byte counts of files.

Syntax:

wc [options] [file...]

Example Command:

wc -l -w -c info.txt

Expected Output:

  6  14  82 info.txt

Flag & Command Breakdown:

  • wc: Word count utility.
  • -l: Prints the newline count.
  • -w: Prints the word count.
  • -c: Prints the byte count.
  • info.txt: Target file name.

7.8 Sorting Text Files: The sort Command

The sort command sorts lines of text files alphabetically or numerically. It does not modify the source file; it prints the sorted result to standard output.

Syntax:

sort [options] [file...]

Example Command:

sort -n -r numbers.txt

Expected Output:

102
89
42
12
5

Flag & Command Breakdown:

  • sort: Sort utility.
  • -n: Numeric sort. Prevents 102 from sorting before 12 (which happens in alphabetical sorting).
  • -r: Reverses the sort order to descending.

7.9 Removing Duplicate Lines: The uniq Command

The uniq command filters adjacent duplicate lines from a sorted stream.

[!IMPORTANT] uniq only detects duplicates that are next to each other. You must always sort the data first (e.g. using sort | uniq).

Syntax:

uniq [options] [input_file [output_file]]

Example Command:

sort usernames.txt | uniq -c

Expected Output:

      1 admin
      3 suresh
      2 guest

Flag & Command Breakdown:

  • uniq: Removes duplicate lines from sorted inputs.
  • -c: Prefixes each line with the number of times it occurred.

7.10 Cutting Out Fields: The cut Command

The cut command extracts sections from each line of a file, splitting columns by bytes, characters, or field delimiters.

Syntax:

cut [options] [file...]

Example Command:

cut -d':' -f1,7 /etc/passwd

Expected Output:

root:/bin/bash
daemon:/usr/sbin/nologin
suresh:/bin/bash

Flag & Command Breakdown:

  • cut: Extracts selected fields from lines.
  • -d':': Sets the field delimiter to a colon (the default delimiter is a tab).
  • -f1,7: Extracts only fields 1 (username) and 7 (shell path).

Chapter 8: The grep Command

8.1 Introduction to Regular Expressions and grep

grep stands for Global Regular Expression Print. It searches input files for lines matching a regular expression pattern and prints them.

  • Regular Expressions (Regex): Special character strings representing search patterns. E.g., ^ matches start of line, $ matches end of line, . matches any single character.

8.2 Basic Usage: Searching for Patterns in Files

Search for a literal word in one or more files.

Syntax:

grep [options] pattern [file...]

Example Command:

grep "debug" config.txt

Expected Output:

debug=true

Flag & Command Breakdown:

  • grep: Search utility.
  • "debug": Literal pattern string.
  • config.txt: File path.

8.3 Case-Insensitive Search: grep -i

Allows searching without distinguishing between uppercase and lowercase letters.

Syntax:

grep -i pattern file

Example Command:

grep -i "Suresh" usernames.txt

Expected Output:

suresh
Suresh
SURESH

Flag & Command Breakdown:

  • -i: Ignores case distinctions in both the pattern and the input files.

8.4 Inverting Matches: grep -v

Prints all lines that do not match the specified pattern. Extremely useful for filtering out noise (like comments or empty lines).

Syntax:

grep -v pattern file

Example Command:

grep -v "^#" config.txt

Expected Output:

port=8080
debug=true
timeout=30

Flag & Command Breakdown:

  • -v: Inverts match selection.
  • "^#": Pattern matching lines that start with a hash symbol (comments).

8.5 Counting Matches: grep -c

Instead of displaying matching lines, grep -c outputs the total count of matching lines.

Syntax:

grep -c pattern file

Example Command:

grep -c "/bin/bash" /etc/passwd

Expected Output:

2

8.6 Displaying Line Numbers: grep -n

Displays the line numbers of matching lines in their original file.

Syntax:

grep -n pattern file

Example Command:

grep -n "debug" config.txt

Expected Output:

2:debug=true

Flag & Command Breakdown:

  • -n: Prefixes each line of output with its 1-indexed line number in the source file.

8.7 Recursive Directory Search: grep -r vs grep -R

  • -r: Search all files under each directory recursively, ignoring symbolic links.
  • -R: Search all files recursively and follow all symbolic links.

Syntax:

grep -r pattern directory

Example Command:

grep -r "docker" /home/suresh/Documents/TechBlog/src/content/blog/

Expected Output:

/home/suresh/Documents/TechBlog/src/content/blog/installing-docker-on-ubuntu.md:Docker has revolutionized...
/home/suresh/Documents/TechBlog/src/content/blog/installing-docker-on-ubuntu.md:sudo apt-get remove docker...

Flag & Command Breakdown:

  • -r: Recursive search flag.

8.8 Matching Exact Words: grep -w

Ensures the match is a standalone word (bounded by spaces, symbols, or line boundaries), rather than a substring of a larger word.

Syntax:

grep -w pattern file

Example Command:

grep -w "log" config.txt

(Will match “log” but will ignore “login” or “logger”)

Flag & Command Breakdown:

  • -w: Forces pattern matches to match only whole words.

8.9 Using Extended Regular Expressions: grep -E (egrep)

Enables Extended Regular Expressions (ERE), allowing use of characters like | (OR), + (1 or more), and ? (0 or 1) without escaping them.

Syntax:

grep -E "pattern1|pattern2" file

Example Command:

grep -E "port|timeout" config.txt

Expected Output:

port=8080
timeout=30

Flag & Command Breakdown:

  • -E: Interprets pattern as an extended regular expression (equivalent to command egrep).

8.10 Printing Only Matching Text: grep -o

Prints only the exact matching substring instead of the entire line.

Syntax:

grep -o pattern file

Example Command:

grep -oE "[0-9]+" config.txt

Expected Output:

8080
30

Flag & Command Breakdown:

  • -o: Prints only the matching part of a line.
  • -E: Extended regex.
  • "[0-9]+": Pattern matching one or more digits.

Chapter 9: Stream Editing with sed

9.1 Introduction to sed (Stream Editor)

sed is a stream editor that parses and transforms text in a single pass. It reads input line-by-line, copies each line into a temporary buffer (the “pattern space”), applies editing commands, and prints the result.

9.2 Substituting Text: The s Command

The most common operation is text substitution: s/pattern/replacement/.

Syntax:

sed 's/pattern/replacement/' file

Example Command:

sed 's/true/false/' config.txt

Expected Output:

port=8080
debug=false
timeout=30

Flag & Command Breakdown:

  • sed: Stream editor.
  • s/true/false/: Substitutes the first match of “true” on a line with “false”.

9.3 Global Substitutions: s/pattern/replacement/g

By default, sed only replaces the first occurrence on each line. Append the g flag for a global replacement.

Syntax:

sed 's/pattern/replacement/g' file

Example Command:

echo "apple banana apple" | sed 's/apple/orange/g'

Expected Output:

orange banana orange

9.4 Editing Files In-Place: sed -i

By default, sed writes output to stdout. The -i option edits the file directly (in-place).

[!CAUTION] In-place replacement modifies files permanently. When writing script scripts, it is safest to create a backup file using -i.bak.

Syntax:

sed -i.bak 's/pattern/replacement/' file

Example Command:

sed -i.bak 's/8080/9000/' config.txt

(Modifies config.txt and creates a backup named config.txt.bak with the original content)

9.5 Deleting Lines: The d Command

Deletes lines matching a pattern or within a line range.

Syntax:

sed 'line_range_or_pattern d' file

Example Command:

sed '/^debug/d' config.txt

Expected Output:

port=8080
timeout=30

Flag & Command Breakdown:

  • /^debug/d: Deletes any line containing a pattern matching “debug” at the start.

9.6 Printing Specific Lines: The p Command and sed -n

By default, sed prints every line. To print only modified or matched lines, combine the p command with the -n (silent) flag.

Syntax:

sed -n 'address p' file

Example Command:

sed -n '2p' config.txt

Expected Output:

debug=true

Flag & Command Breakdown:

  • -n: Suppresses automatic printing of the pattern space.
  • 2p: Prints only the 2nd line of the file.

9.7 Running Multiple sed Commands: sed -e

Use the -e option to chain multiple editing expressions in a single command.

Syntax:

sed -e 'cmd1' -e 'cmd2' file

Example Command:

sed -e 's/true/false/' -e 's/8080/9000/' config.txt

Expected Output:

port=9000
debug=false
timeout=30

9.8 Restricting sed to Specific Lines (Address Matching)

You can restrict any sed operation to a specific line number or line range.

Syntax:

sed 'start_line,end_line command' file

Example Command:

sed '1,2 s/=/ : /' config.txt

Expected Output:

port : 8080
debug : true
timeout=30

(Applies the substitute command only to lines 1 through 2)

9.9 Using Alternate Delimiters

If you are editing file paths or URLs, standard forward slash delimiters (/) clash and must be escaped (\/). Avoid this by using alternate delimiters like #, _, or @.

Syntax:

sed 's#pattern#replacement#' file

Example Command:

echo "/usr/local/bin" | sed 's#/usr/local#/opt#g'

Expected Output:

/opt/bin

9.10 Backreferences in sed Substitutions

Backreferences allow you to capture matching groups using \( \) and reuse them in the replacement string as \1, \2, etc.

Syntax:

sed 's/\(pattern\)/\1-suffix/' file

Example Command:

echo "port=8080" | sed 's/port=\([0-9]\+\)/server_port=\1/'

Expected Output:

server_port=8080

Flag & Command Breakdown:

  • \([0-9]\+\): Captures the digits 8080 into capture group 1.
  • \1: Inserts the captured group 1 value in the replacement.