+
+
+
+
+
+
+
elixir
rb
+
+
rest
=
โІ
preact
+
swift
express
mysql
next
lisp
+
+
mint
+
cdn
+
+
+
+
hugging
fiber
+
+
koa
+
+
+
+
+
c
>=
+
&
apex
+
+
+
+
junit
+
+
+
::
+
sinatra
+
+
soap
redis
โˆ‘
nomad
+
neo4j
supabase
gulp
+
+
objc
fastapi
+
+
->
fiber
chef
surrealdb
vercel
+
webpack
+
vb
*
+
+
+
aurelia
+
+
+
Back to Blog
๐Ÿ“Š Monitoring File System Usage: Simple Guide
Alpine Linux File System Disk Monitoring

๐Ÿ“Š Monitoring File System Usage: Simple Guide

Published Jun 3, 2025

Easy tutorial for monitoring disk space and file system usage on Alpine Linux. Perfect for beginners with step-by-step instructions and clear examples.

9 min read
0 views
Table of Contents

๐Ÿ“Š Monitoring File System Usage: Simple Guide

Letโ€™s learn to monitor disk space on Alpine Linux! This is super useful! ๐ŸŽ‰ Weโ€™ll check how much storage youโ€™re using. Keep your system healthy! ๐Ÿ˜Š

๐Ÿค” What is File System Monitoring?

File system monitoring watches your disk space and storage. Think of it like checking how full your closet is!

Monitoring helps with:

  • ๐Ÿ“ˆ Tracking disk space usage
  • ๐Ÿšจ Warning when space gets low
  • ๐Ÿ”ง Finding what uses most space

๐ŸŽฏ What You Need

Before we start, you need:

  • โœ… Alpine Linux system running
  • โœ… Basic terminal knowledge
  • โœ… Root access for some commands
  • โœ… Understanding of file paths

๐Ÿ“‹ Step 1: Basic Disk Space Commands

Using df Command

Letโ€™s start with the most basic command! Itโ€™s really easy! ๐Ÿ˜Š

What weโ€™re doing: Checking overall disk space usage.

# Check disk space in human-readable format
df -h

# Check disk space for specific filesystem
df -h /

# Show all filesystem types
df -T

# Check inodes usage
df -i

What this does: ๐Ÿ“– Shows how much space is used and available on each disk.

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  3.2G   15G  18% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
/dev/sda2       100G   45G   50G  48% /home

What this means: Your root filesystem has 18% usage - thatโ€™s healthy! โœ…

๐Ÿ’ก Important Tips

Tip: Always monitor disk usage regularly! ๐Ÿ’ก

Warning: When usage reaches 90%, take action quickly! โš ๏ธ

๐Ÿ› ๏ธ Step 2: Detailed File Usage Analysis

Using du Command

Now letโ€™s find whatโ€™s using the most space! This is like detective work! ๐ŸŽฏ

What weโ€™re doing: Finding which files and folders use most disk space.

# Check current directory usage
du -h

# Check usage summary for current directory
du -sh

# Check top 10 largest directories
du -h / | sort -hr | head -10

# Check specific directory usage
du -sh /var/log

# Show usage for each subdirectory
du -h --max-depth=1 /home

Code explanation:

  • du -h: Shows sizes in human-readable format (KB, MB, GB)
  • du -sh: Shows total summary only
  • sort -hr: Sorts by size, largest first
  • --max-depth=1: Shows only one level deep

Expected Output:

1.2G    /var/lib
800M    /usr/lib
512M    /var/log
256M    /home/user
128M    /tmp

What this means: Now you know where your space is going! ๐ŸŒŸ

๐Ÿ“Š Quick Summary Table

CommandPurposeResult
๐Ÿ“Š df -hOverall disk usageโœ… Shows free space
๐Ÿ” du -shDirectory sizeโœ… Shows folder usage
๐Ÿ“ˆ ncduInteractive browserโœ… Visual disk explorer
๐Ÿšจ watch df -hReal-time monitoringโœ… Live updates

๐ŸŽฎ Step 3: Advanced Monitoring Tools

Installing ncdu (Interactive Disk Usage)

Letโ€™s install a super cool visual tool! Youโ€™ll love this! ๐ŸŒŸ

What weโ€™re doing: Installing an interactive disk usage analyzer.

# Install ncdu (NCurses Disk Usage)
apk add ncdu

# Run ncdu on root filesystem
ncdu /

# Run ncdu on specific directory
ncdu /home

# Run ncdu with real-time scan
ncdu -x /var

What this does: Gives you a visual, interactive way to explore disk usage! ๐Ÿ“š

Using watch for Real-time Monitoring

What weโ€™re doing: Setting up continuous monitoring that updates automatically.

# Watch disk usage every 2 seconds
watch -n 2 df -h

# Watch specific directory size
watch -n 5 "du -sh /var/log"

# Watch multiple locations
watch -n 10 "df -h; echo; du -sh /tmp /var/log /home"

# Save watch output to file
watch -n 30 "df -h >> /var/log/disk-usage.log"

Expected Output:

Every 2.0s: df -h                Mon Jun  3 16:30:15 2025

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  3.3G   15G  19% /
tmpfs           2.0G   12M  2.0G   1% /dev/shm

What this means: Your disk usage updates live! Perfect for monitoring! ๐ŸŽ‰

๐ŸŽฎ Practice Time!

Letโ€™s practice what you learned! Try these simple examples:

Example 1: Find Large Files ๐ŸŸข

What weโ€™re doing: Finding the biggest files on your system.

# Find files larger than 100MB
find / -size +100M -type f -exec ls -lh {} \; 2>/dev/null | head -10

# Find largest files in /var
find /var -type f -exec ls -s {} \; | sort -nr | head -10

# Find large files in current directory
ls -lSh | head -10

# Create a report of large files
find / -size +50M -type f -exec ls -lh {} \; 2>/dev/null > /tmp/large-files.txt

echo "Large files report saved to /tmp/large-files.txt โœ…"

What this does: Helps you find space hogs quickly! ๐ŸŒŸ

Example 2: Set Up Automated Monitoring ๐ŸŸก

What weโ€™re doing: Creating scripts that monitor disk usage automatically.

# Create disk monitoring script
cat > /usr/local/bin/disk-monitor.sh << 'EOF'
#!/bin/sh
# Disk Usage Monitor Script

LOG_FILE="/var/log/disk-monitor.log"
ALERT_THRESHOLD=80

log_message() {
    echo "$(date): $1" | tee -a "$LOG_FILE"
}

# Check disk usage for root filesystem
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')

log_message "Disk usage: ${USAGE}%"

if [ "$USAGE" -gt "$ALERT_THRESHOLD" ]; then
    log_message "WARNING: Disk usage is ${USAGE}% - exceeds threshold!"
    
    # Show top space users
    log_message "Top space users:"
    du -sh /* 2>/dev/null | sort -hr | head -5 | tee -a "$LOG_FILE"
    
    # Clean temporary files if needed
    if [ "$USAGE" -gt 90 ]; then
        log_message "CRITICAL: Cleaning temporary files"
        find /tmp -type f -mtime +7 -delete
        find /var/log -name "*.log" -mtime +30 -delete
    fi
else
    log_message "Disk usage normal"
fi
EOF

# Make script executable
chmod +x /usr/local/bin/disk-monitor.sh

# Test the script
/usr/local/bin/disk-monitor.sh

# Set up cron job to run every hour
echo "0 * * * * /usr/local/bin/disk-monitor.sh" | crontab -

echo "Automated disk monitoring configured! ๐Ÿ“š"

What this does: Monitors your disk automatically and cleans up when needed! ๐Ÿ“š

๐Ÿšจ Fix Common Problems

Problem 1: Disk suddenly full โŒ

What happened: Your disk space disappeared quickly. How to fix it: Find and clean the culprit!

# Find what filled up recently
find / -type f -mtime -1 -size +10M 2>/dev/null

# Check log files
du -sh /var/log/*

# Clean old log files
find /var/log -name "*.log" -mtime +7 -delete

# Clear package cache
apk cache clean

Problem 2: Canโ€™t find space users โŒ

What happened: Disk shows full but canโ€™t find large files. How to fix it: Check for hidden files and deleted files!

# Check for hidden files
du -sh .[^.]* * 2>/dev/null

# Check for deleted files still open
lsof +L1

# Check system directories
du -sh /var/* /usr/* /opt/* 2>/dev/null | sort -hr

Problem 3: Monitoring not working โŒ

What happened: Scripts donโ€™t run automatically. How to fix it: Check cron and permissions!

# Check if cron is running
rc-service crond status

# Start cron if needed
rc-service crond start

# Check cron logs
tail -f /var/log/cron.log

# Test script manually
bash -x /usr/local/bin/disk-monitor.sh

Donโ€™t worry! Disk monitoring takes practice. Youโ€™re doing great! ๐Ÿ’ช

๐Ÿ’ก Simple Tips

  1. Monitor daily ๐Ÿ“… - Check disk space regularly
  2. Set alerts ๐ŸŒฑ - Know before you run out
  3. Clean regularly ๐Ÿค - Remove old files often
  4. Use tools ๐Ÿ’ช - Visual tools make it easier

โœ… Check Everything Works

Letโ€™s verify monitoring is working:

# Check current disk usage
df -h

# Test ncdu if installed
which ncdu && echo "ncdu available โœ…"

# Check monitoring script
test -x /usr/local/bin/disk-monitor.sh && echo "Monitor script ready โœ…"

# Check cron job
crontab -l | grep disk-monitor && echo "Cron job configured โœ…"

# View recent monitoring logs
tail -5 /var/log/disk-monitor.log

echo "File system monitoring verification completed! โœ…"

Good output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  3.3G   15G  19% /
ncdu available โœ…
Monitor script ready โœ…
Cron job configured โœ…
Mon Jun  3 16:45:01 2025: Disk usage normal
File system monitoring verification completed! โœ…

๐Ÿ”ง Advanced Monitoring Features

Setting Up Disk Usage Alerts

Letโ€™s create email alerts when disk space gets low! This is professional! ๐ŸŽฏ

What weโ€™re doing: Creating smart alerts that notify you of disk issues.

# Install mail utilities
apk add mailx

# Create advanced monitoring script
cat > /usr/local/bin/advanced-disk-monitor.sh << 'EOF'
#!/bin/sh
# Advanced Disk Usage Monitor

CONFIG_FILE="/etc/disk-monitor.conf"
LOG_FILE="/var/log/disk-monitor.log"

# Default configuration
WARNING_THRESHOLD=75
CRITICAL_THRESHOLD=90
EMAIL_ALERTS=true
ADMIN_EMAIL="[email protected]"

# Load configuration if exists
[ -f "$CONFIG_FILE" ] && . "$CONFIG_FILE"

log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" | tee -a "$LOG_FILE"
}

send_alert() {
    local subject="$1"
    local message="$2"
    
    if [ "$EMAIL_ALERTS" = "true" ] && command -v mail >/dev/null; then
        echo "$message" | mail -s "$subject" "$ADMIN_EMAIL"
    fi
}

check_filesystem() {
    local fs="$1"
    local usage=$(df "$fs" | tail -1 | awk '{print $5}' | sed 's/%//')
    local used=$(df -h "$fs" | tail -1 | awk '{print $3}')
    local avail=$(df -h "$fs" | tail -1 | awk '{print $4}')
    
    log_message "Filesystem $fs: ${usage}% used (${used} used, ${avail} available)"
    
    if [ "$usage" -ge "$CRITICAL_THRESHOLD" ]; then
        log_message "CRITICAL: Filesystem $fs usage at ${usage}%"
        send_alert "CRITICAL: Disk Space Alert" \
            "Filesystem $fs usage is critically high at ${usage}%\nUsed: $used\nAvailable: $avail"
        
        # Emergency cleanup
        emergency_cleanup "$fs"
        
    elif [ "$usage" -ge "$WARNING_THRESHOLD" ]; then
        log_message "WARNING: Filesystem $fs usage at ${usage}%"
        send_alert "WARNING: Disk Space Alert" \
            "Filesystem $fs usage is approaching limits at ${usage}%\nUsed: $used\nAvailable: $avail"
    fi
}

emergency_cleanup() {
    local fs="$1"
    log_message "Starting emergency cleanup for $fs"
    
    # Clean temporary files
    find /tmp -type f -mtime +1 -delete 2>/dev/null
    
    # Compress old logs
    find /var/log -name "*.log" -mtime +7 -exec gzip {} \; 2>/dev/null
    
    # Clean package cache
    apk cache clean
    
    log_message "Emergency cleanup completed"
}

# Check all mounted filesystems
df | awk 'NR>1 {print $6}' | while read filesystem; do
    check_filesystem "$filesystem"
done

# Generate usage report
log_message "=== Disk Usage Summary ==="
df -h | tee -a "$LOG_FILE"
EOF

chmod +x /usr/local/bin/advanced-disk-monitor.sh

# Create configuration file
cat > /etc/disk-monitor.conf << 'EOF'
# Disk Monitor Configuration
WARNING_THRESHOLD=75
CRITICAL_THRESHOLD=90
EMAIL_ALERTS=true
ADMIN_EMAIL="[email protected]"
EOF

# Test advanced monitoring
/usr/local/bin/advanced-disk-monitor.sh

What this does: Creates professional-grade disk monitoring with alerts! ๐ŸŒŸ

Creating Disk Usage Dashboard

What weโ€™re doing: Building a simple web dashboard to view disk usage.

# Create web dashboard script
cat > /usr/local/bin/disk-usage-web.sh << 'EOF'
#!/bin/sh
# Disk Usage Web Dashboard Generator

WEB_DIR="/var/www/html"
HTML_FILE="$WEB_DIR/disk-usage.html"

mkdir -p "$WEB_DIR"

cat > "$HTML_FILE" << 'HTML'
<!DOCTYPE html>
<html>
<head>
    <title>Alpine Linux Disk Usage Dashboard</title>
    <meta http-equiv="refresh" content="60">
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .header { background: #2c3e50; color: white; padding: 20px; border-radius: 5px; }
        .metric { background: #ecf0f1; padding: 15px; margin: 10px 0; border-radius: 5px; }
        .warning { background: #f39c12; color: white; }
        .critical { background: #e74c3c; color: white; }
        pre { background: #34495e; color: white; padding: 15px; border-radius: 5px; overflow-x: auto; }
    </style>
</head>
<body>
    <div class="header">
        <h1>๐Ÿ–ฅ๏ธ Alpine Linux Disk Usage Dashboard</h1>
        <p>Last updated: $(date)</p>
    </div>
HTML

# Add filesystem information
echo '<div class="metric">' >> "$HTML_FILE"
echo '<h2>๐Ÿ“Š Filesystem Usage</h2>' >> "$HTML_FILE"
echo '<pre>' >> "$HTML_FILE"
df -h >> "$HTML_FILE"
echo '</pre>' >> "$HTML_FILE"
echo '</div>' >> "$HTML_FILE"

# Add top space users
echo '<div class="metric">' >> "$HTML_FILE"
echo '<h2>๐Ÿ” Top Space Users</h2>' >> "$HTML_FILE"
echo '<pre>' >> "$HTML_FILE"
du -sh /* 2>/dev/null | sort -hr | head -10 >> "$HTML_FILE"
echo '</pre>' >> "$HTML_FILE"
echo '</div>' >> "$HTML_FILE"

# Close HTML
cat >> "$HTML_FILE" << 'HTML'
</body>
</html>
HTML

echo "Dashboard updated: $HTML_FILE"
EOF

chmod +x /usr/local/bin/disk-usage-web.sh

# Set up automatic dashboard updates
echo "*/5 * * * * /usr/local/bin/disk-usage-web.sh" | crontab -

# Generate initial dashboard
/usr/local/bin/disk-usage-web.sh

echo "Web dashboard created! View at file://$WEB_DIR/disk-usage.html"

Expected Output:

Dashboard updated: /var/www/html/disk-usage.html
Web dashboard created! View at file:///var/www/html/disk-usage.html
โœ… Professional monitoring dashboard ready

What this means: You now have a web dashboard for monitoring! ๐ŸŽ‰

๐Ÿ† What You Learned

Great job! Now you can:

  • โœ… Monitor disk space with df and du commands
  • โœ… Use interactive tools like ncdu for visual exploration
  • โœ… Set up automated monitoring with alerts
  • โœ… Create professional dashboards for disk usage!

๐ŸŽฏ Whatโ€™s Next?

Now you can try:

  • ๐Ÿ“š Learning advanced filesystem tools like iotop
  • ๐Ÿ› ๏ธ Setting up centralized monitoring with Prometheus
  • ๐Ÿค Creating automated disk cleanup policies
  • ๐ŸŒŸ Building custom monitoring solutions!

Remember: Every system administrator was once a beginner. Youโ€™re doing amazing! ๐ŸŽ‰

Keep practicing and youโ€™ll become an expert too! ๐Ÿ’ซ