Loops

Loops

Concepts

for Loop

Iterating Over a List

for item in value1 value2 value3; do
    echo "$item"
done
# Loop over words
for color in red green blue; do
    echo "Color: $color"
done

# Loop over files
for file in /etc/*.conf; do
    echo "Config file: $file"
done

# Loop over command output
for user in $(cut -d: -f1 /etc/passwd | head -5); do
    echo "User: $user"
done

# Loop over array elements
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done

# Loop over arguments
for arg in "$@"; do
    echo "Argument: $arg"
done

C-Style for Loop

for ((i = 0; i < 10; i++)); do
    echo "i = $i"
done

# Count by 2
for ((i = 0; i <= 20; i += 2)); do
    echo "$i"
done

Brace Expansion

# Range of numbers
for i in {1..5}; do
    echo "Number: $i"
done

# Range with step
for i in {0..100..10}; do
    echo "$i"
done

# Letters
for letter in {a..z}; do
    echo "$letter"
done

Note: Brace expansion {1..5} is a Bash feature. It does not work in sh/dash.

[Read more]

LVM and Swap

LVM and Swap

Concepts

LVM — Logical Volume Manager

Regular partitions are rigid — you cannot easily resize them or span them across disks. LVM adds a flexible layer between physical disks and filesystems.

LVM Architecture

Physical disks:   /dev/sda    /dev/sdb
                      │           │
Physical Volumes: /dev/sda1   /dev/sdb1       ← PV (raw storage)
                      │           │
Volume Group:     ════════ vg_data ═══════    ← VG (pool of storage)
                      │           │
Logical Volumes:  lv_home     lv_var          ← LV (virtual partitions)
                      │           │
Filesystems:      ext4        xfs             ← mounted like regular partitions
  • Physical Volume (PV) — a partition or whole disk assigned to LVM
  • Volume Group (VG) — a pool of storage made from one or more PVs
  • Logical Volume (LV) — a virtual partition carved from a VG (this is what you format and mount)

LVM Advantages

  • Resize: grow or shrink logical volumes without repartitioning
  • Span disks: combine multiple physical disks into one volume group
  • Snapshots: create point-in-time copies of a volume
  • Move data: migrate data between physical disks without downtime

LVM Commands

# Install LVM tools
sudo apt install -y lvm2

# --- Physical Volumes ---
sudo pvcreate /dev/sdb1                # initialize a PV
sudo pvs                               # list PVs
sudo pvdisplay                         # detailed PV info

# --- Volume Groups ---
sudo vgcreate vg_data /dev/sdb1        # create a VG from PV(s)
sudo vgextend vg_data /dev/sdc1        # add another PV to the VG
sudo vgs                               # list VGs
sudo vgdisplay                         # detailed VG info

# --- Logical Volumes ---
sudo lvcreate -L 10G -n lv_home vg_data     # create 10GB LV
sudo lvcreate -l 100%FREE -n lv_data vg_data # use all remaining space
sudo lvs                                     # list LVs
sudo lvdisplay                               # detailed LV info

# --- Format and mount ---
sudo mkfs.ext4 /dev/vg_data/lv_home
sudo mount /dev/vg_data/lv_home /home

# --- Resize ---
# Extend a logical volume (add 5GB)
sudo lvextend -L +5G /dev/vg_data/lv_home
# Resize the filesystem to match
sudo resize2fs /dev/vg_data/lv_home    # ext4
# Or for XFS:
# sudo xfs_growfs /mnt/mountpoint

# Extend to fill all free space in VG
sudo lvextend -l +100%FREE /dev/vg_data/lv_home
sudo resize2fs /dev/vg_data/lv_home

Check if You Are Using LVM

lsblk
# LVM volumes show as "lvm" type:
# sda2       8:2    0  38.5G  0 part
# └─vg--sys-lv--root 253:0 0  38.5G  0 lvm  /

sudo lvs 2>/dev/null
sudo vgs 2>/dev/null

Ubuntu’s server installer and Debian’s guided partitioning can optionally set up LVM during installation.

[Read more]

Managing Users and Groups

Managing Users and Groups

Concepts

Groups

Every user belongs to at least one group. Groups simplify permission management — instead of granting access to individual users, you assign them to a group and set permissions on the group.

Each user has:

  • Primary group — assigned when the account is created (usually a group with the same name as the user). New files are owned by this group.
  • Supplementary groups — additional groups the user belongs to for extra permissions.
id kmiguel
# uid=1000(kmiguel) gid=1000(kmiguel) groups=1000(kmiguel),27(sudo),4(adm)
#                    ↑ primary group                      ↑ supplementary groups

The /etc/group File

grep sudo /etc/group
sudo:x:27:kmiguel,alice

Format: groupname:password:GID:members

[Read more]

nano

nano

Concepts

Why You Need a Terminal Text Editor

Many Linux tasks require editing configuration files, scripts, or system files directly from the command line. You cannot always use a graphical editor — especially on servers without a desktop environment, over SSH, or when the desktop is broken.

The two most common terminal editors are nano (simple) and vim (powerful). This lesson covers nano.

What Is nano?

nano is a straightforward, beginner-friendly terminal text editor. It is pre-installed on both Ubuntu and Debian. It shows a list of keyboard shortcuts at the bottom of the screen, so you do not need to memorize anything to get started.

[Read more]

Navigating the Filesystem

Navigating the Filesystem

Concepts

Everything Is a File

In Linux, (almost) everything is represented as a file: regular files, directories, hardware devices, running processes, and even network sockets. This is a fundamental design principle of Unix/Linux systems.

A directory is just a special file that contains a list of other files and directories.

The Directory Tree

Linux has a single directory tree starting from the root directory /. There are no drive letters like C:\ or D:\ on Windows. Everything lives under /:

[Read more]