Functions and Error Handling

Functions and Error Handling

Concepts

Functions

Functions let you group commands under a name and reuse them. They reduce repetition and make scripts easier to read and maintain.

Defining Functions

# Syntax 1 (preferred)
function_name() {
    commands
}

# Syntax 2
function function_name {
    commands
}

Calling Functions

greet() {
    echo "Hello, World!"
}

greet          # call the function
greet          # call it again

Function Arguments

Functions receive arguments the same way scripts do — $1, $2, $@, $#:

[Read more]

grep

grep

Concepts

What Is grep?

grep (Global Regular Expression Print) searches for patterns in text and prints matching lines. It is one of the most frequently used commands on Linux. You use it to find things — in files, in command output, in logs.

grep "pattern" filename
# Prints every line in filename that contains "pattern"

Basic Usage

# Search for a string in a file
grep "error" /var/log/syslog

# Search in multiple files
grep "error" /var/log/*.log

# Search from stdin (piped input)
cat /etc/passwd | grep "bash"
dmesg | grep "usb"

Common Options

Option Meaning Example
-i Case insensitive grep -i "error" log.txt
-n Show line numbers grep -n "TODO" code.py
-c Count matching lines grep -c "error" log.txt
-l List filenames with matches (not the lines) grep -l "password" /etc/*
-L List filenames WITHOUT matches grep -L "error" *.log
-v Invert match (lines that do NOT match) grep -v "^#" config.txt
-r Recursive (search all files in directories) grep -r "TODO" /project/
-w Match whole words only grep -w "log" file.txt (not “login”)
-x Match entire line grep -x "exact line" file.txt
-o Print only the matching part, not the whole line grep -o "[0-9]*" file.txt
-A N Show N lines after each match grep -A 3 "error" log.txt
-B N Show N lines before each match grep -B 2 "error" log.txt
-C N Show N lines before and after (context) grep -C 5 "error" log.txt
--color Highlight matches (usually default) grep --color "pattern" file

Combining Options

Options can be combined freely:

[Read more]

Installing Linux

Installing Linux

Concepts

Choosing Your Installation Method

There are several ways to run Linux. For learning, a virtual machine is the safest and most practical option: you can experiment freely without risking your main operating system.

Method Pros Cons Best For
Virtual Machine (VM) Safe, reversible, snapshots Slightly slower, needs 4+ GB RAM to spare Learning (recommended)
Dual Boot Full hardware performance Risk of overwriting existing OS if done wrong Daily use alongside Windows
WSL2 (Windows) Fast, no reboot needed, integrated No real systemd (workaround exists), no desktop Quick CLI access from Windows
Bare Metal Full performance, full control Wipes existing OS (unless dual-boot) Dedicated Linux machine

This lesson walks through the VirtualBox VM approach, with brief notes on dual-boot and WSL2.

[Read more]

Keys, Config, and Tunnels

Keys, Config, and Tunnels

Concepts

SSH Key Authentication

Key-based auth is more secure than passwords: the private key never leaves your machine, and it can be protected with a passphrase.

Generate a Key Pair

ssh-keygen -t ed25519 -C "[email protected]"
# -t ed25519: modern, fast, secure algorithm
# -C: comment (usually your email)

It asks for:

  • File: default ~/.ssh/id_ed25519 (press Enter)
  • Passphrase: encrypts the private key (recommended — adds a second factor)

This creates two files:

[Read more]

Logs and journalctl

Logs and journalctl

Concepts

Why Logs Matter

When something goes wrong on a Linux system (— )a service crashes, a login fails, a disk fills up, the gpu caught fire and is now burning your house down…) the answer is almost always in the logs. Logs record what happened, when, and often why.

Linux has two logging systems that work together:

  1. systemd journal (journalctl) — collects logs from all systemd services, the kernel, and system messages. Binary format, fast queries, structured data.
  2. Traditional log files (/var/log/) — text files written by rsyslog or syslog-ng. Older but still widely used.

/var/log/ — Traditional Log Files

ls /var/log/
File Content
syslog General system messages (Ubuntu)
messages General system messages (Debian, if configured)
auth.log Authentication events (logins, sudo, SSH)
kern.log Kernel messages
dpkg.log Package installation/removal history
apt/history.log APT command history
apt/term.log APT terminal output
boot.log Boot messages
dmesg Kernel ring buffer (hardware, drivers)
faillog Failed login attempts
lastlog Last login for each user
wtmp Login/logout history (binary, read with last)
# View recent syslog entries
sudo tail -20 /var/log/syslog

# Follow syslog in real time
sudo tail -f /var/log/syslog

# Search for SSH events
sudo grep "sshd" /var/log/auth.log | tail -10

# View package install history
cat /var/log/dpkg.log | tail -20

Log Rotation

Log files grow continuously. logrotate automatically compresses and rotates old logs:

[Read more]