Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

LX
Linux
0/11 completed
Beginner
The Linux file systemUsers, groups and permissionsPackage managers
Intermediate
Processes and signalsDisks, storage and archivesNetworking commandsSSH and remote access
Advanced
systemd and servicesLogs and troubleshooting
Pro
Securing a serverPerformance and resource limits
Tracks / Linux / Intermediate

Disks, storage and archives

"No space left on device" takes down more websites than you'd expect: logs grow, uploads pile up, a database can't write, and everything stops. This lesson covers how Linux sees disks, how to find what's using space, how to add a new disk, and how to pack files into archives for backups and transfers.

Free space: df

bash
df -h /
echo "---"
df -i / | tail -1 | awk '{ print "inodes used:", $5 }'
Output
Filesystem      Size  Used Avail Use% Mounted on
overlay         224G   15G  199G   7% /
---
inodes used: 2%
  • Filesystem is the device. Here it's overlay because these examples ran in a container; on a server you'll see something like /dev/nvme0n1p1 or /dev/sda1.
  • -h shows human-readable sizes (G, M).
  • Use% is the number to watch. Set an alert at around 80%.
  • df -i shows inodes: every file needs one, and a disk can run out of them while it still has free space, usually because of millions of tiny files (cache or session folders).

What's using the space: du

bash
sudo du -h --max-depth=1 /var 2>/dev/null | sort -h | tail -5
echo "---"
sudo find /var -type f -size +1M -exec du -h {} + 2>/dev/null | sort -h | tail -3
Output
16K	/var/spool
1.8M	/var/cache
8.7M	/var/lib
17M	/var/log
28M	/var
---
8.0M	/var/log/journal/6758c97f8bef4bfcb49babbd1afc0da2/system.journal
8.0M	/var/log/journal/6758c97f8bef4bfcb49babbd1afc0da2/user-1001.journal

Start at /, find the biggest folder, go into it, repeat. ncdu (sudo apt install ncdu) does the same interactively and is a joy on a full disk.

The usual suspects

Where What Fix
/var/log Logs that are never rotated logrotate (see Logs and troubleshooting), journalctl --vacuum-size=500M
/var/lib/docker Old images, stopped containers, build cache docker system prune
/var/cache/apt Downloaded packages sudo apt clean
/tmp, app upload folders Forgotten temporary files A cleanup cron job or systemd timer
Home folders Downloads, old backups Ask the owner

Deleted files that still take space

If you delete a big log file while a program still has it open, the space is not freed until that program closes it. df says the disk is full, but du can't find the files:

bash
sudo bash -c 'exec 3> /var/log/huge.log; head -c 50M /dev/zero >&3; rm /var/log/huge.log; sleep 30' &
sleep 1
sudo lsof -nP +L1 2>/dev/null | awk 'NR == 1 || /huge/ { print $1, $7, $NF, $(NF-1) }'
Output
COMMAND SIZE/OFF NAME NODE
sleep 52428800 (deleted) /var/log/huge.log

lsof +L1 lists open files with zero links, i.e. deleted. Restart (or signal) that process to release the space. To empty a log a program is still writing to, truncate it instead of deleting it: sudo truncate -s 0 /var/log/app.log.

Block devices: lsblk

Disks appear as block devices in /dev:

Name Device
/dev/sda, /dev/sdb SATA/SCSI disks (and most cloud disks on older VMs)
/dev/nvme0n1 NVMe SSDs (and AWS/GCP disks on modern instances)
/dev/vda Virtual disks on KVM-based clouds
/dev/sda1, /dev/nvme0n1p1 Partitions on those disks

lsblk lists them as a tree. On a typical cloud server it looks like this (sample output from an AWS instance with an extra 100 GB data disk that hasn't been set up yet):

NAME          SIZE TYPE MOUNTPOINTS
nvme0n1        30G disk
├─nvme0n1p1    29G part /
├─nvme0n1p15  106M part /boot/efi
└─nvme0n1p16  913M part /boot
nvme1n1       100G disk

Adding a new disk

To use a new disk you: create a file system on it, mount it on a folder, and add it to /etc/fstab so it's mounted again after a reboot. Here's the whole process for real, using a file as a pretend 200 MB disk (a loop device):

bash
truncate -s 200M data.img                       # pretend this is /dev/nvme1n1
sudo mkfs.ext4 -q -L appdata data.img           # 1. create an ext4 file system
sudo mkdir -p /mnt/data
sudo mount -o loop data.img /mnt/data           # 2. mount it
df -h /mnt/data | tail -1 | awk '{ print $2, "total,", $4, "free, mounted on", $6 }'
sudo chown you: /mnt/data
echo "important" > /mnt/data/orders.csv
ls /mnt/data
Output
172M total, 158M free, mounted on /mnt/data
lost+found  orders.csv

The lost+found folder is created by ext4 for repairing damaged files. On a real disk the steps are the same: sudo mkfs.ext4 /dev/nvme1n1, then sudo mount /dev/nvme1n1 /mnt/data.

⚠️ mkfs erases the device. Triple-check the name with lsblk before running it. Formatting the wrong disk is one of the classic unrecoverable mistakes.

Mounting at boot: /etc/fstab

Mounts made with mount disappear after a reboot. /etc/fstab lists the ones to restore:

# device                                   mount point  type  options          dump pass
UUID=0b8c3b4e-6a7d-4d0f-9d43-0e9a3c5f7b21  /mnt/data    ext4  defaults,nofail  0    2
  • Use the UUID (from sudo blkid), not /dev/sdb: device names can change between boots.
  • nofail lets the server boot even if this disk is missing. Without it, a missing data disk can leave the server stuck at boot.
  • Test with sudo mount -a (mounts everything in fstab) before you reboot.
bash
sync
sudo umount /mnt/data
mount | grep -c /mnt/data
sudo mount -o loop data.img /mnt/data && cat /mnt/data/orders.csv
Output
^@0
important

umount (no "n") detaches it. "target is busy" means some process is still using a file there; sudo lsof +f -- /mnt/data tells you which.

File systems you'll meet

Type Used for
ext4 The default on Ubuntu and Debian. Reliable, everywhere
xfs The default on RHEL. Great for large files and big disks
btrfs, zfs Snapshots, checksums, compression
vfat The EFI boot partition, USB sticks
tmpfs Lives in RAM: fast, gone at reboot (/run, often /tmp)
nfs, cifs Network shares
overlay Docker containers' layered file systems

Archives and compression

Linux separates bundling files into one (tar) from compressing them (gzip, xz, zstd). tar can do both at once:

bash
mkdir -p site/css site/img
for i in $(seq 1 200); do echo "<p>Line $i of a fairly repetitive page</p>"; done > site/index.html
echo "body { margin: 0 }" > site/css/main.css
head -c 20000 /dev/urandom > site/img/photo.jpg
tar -czf site.tar.gz site                  # create (c), gzip (z), file (f)
tar -tzf site.tar.gz                       # list (t) the contents
du -sh site site.tar.gz | sort -h
Output
site/
site/index.html
site/css/
site/css/main.css
site/img/
site/img/photo.jpg
24K	site.tar.gz
48K	site

The repetitive HTML compressed very well; the random "photo" didn't compress at all. Real JPEGs, MP4s and ZIPs are already compressed, so compressing them again gains almost nothing.

bash
mkdir -p restore
tar -xzf site.tar.gz -C restore            # extract (x) into a folder (-C)
ls restore/site
tar -xzf site.tar.gz -C restore site/css/main.css --strip-components=2
ls restore
Output
css  img  index.html
main.css  site
Option Meaning
c / x / t Create / extract / list
f file The archive file name (always last in the group)
z / J / --zstd gzip / xz / zstd compression
-C dir Change to this folder first
v Verbose: print each file
--exclude='*.log' Leave files out

Remember it as create zipped file = czf, extract zipped file = xzf. Modern tar detects the compression by itself when extracting, so tar -xf file.tar.xz works too.

Compressors compared

bash
for i in $(seq 1 20000); do echo "2026-11-22 10:00:$((i % 60)) INFO request served in $((i % 300))ms"; done > app.log
gzip -k app.log && xz -k app.log
ls -l app.log app.log.gz app.log.xz | awk '{ printf "%-12s %8d bytes\n", $NF, $5 }'
zcat app.log.gz | tail -1
Output
app.log        969293 bytes
app.log.gz      14543 bytes
app.log.xz        912 bytes
2026-11-22 10:00:20 INFO request served in 200ms

gzip is fast and works everywhere; xz compresses smaller but slower; zstd is both fast and small and is becoming the new default. zcat, zgrep and zless read .gz files without extracting them, perfect for old rotated logs. zip/unzip are there when you need to share with Windows users.

Practice

  1. Find the three biggest folders inside /usr.
  2. Archive your home folder into home-backup.tar.gz, leaving out any node_modules folders.
  3. List the contents of the archive without extracting it, then extract one file from it.
  4. Create a 100 MB file-backed disk, format it as ext4, mount it on /mnt/test, write a file, unmount and remount it to prove the file survived.
✎ Check your understanding

1.df says the disk is 100% full, but du finds far less data. A likely cause?

2.What does tar -xzf backup.tar.gz -C /srv do?

3.Why use a UUID instead of /dev/sdb in /etc/fstab?

4.What does mkfs.ext4 /dev/sdb do to the data already on /dev/sdb?

← Processes and signals