Raspberry Pi Disk Space Guide
How to Check Raspberry Pi Disk Space: A Command-Line Investigation
Master Raspberry Pi disk space management with essential command-line tools to monitor storage, identify space hogs, and optimize SD card usage.
The Silent Crisis: When Your Raspberry Pi Runs Out of Room
Storage exhaustion on a Raspberry Pi rarely announces itself with fanfare. Instead, it manifests as sluggish performance, failed updates, or sudden service crashes. For devices relying on modest SD cards—often 16GB or 32GB—vigilant disk space management is not optional; it is foundational to system stability. This investigation dissects the precise methods for auditing storage consumption, identifying hidden space consumers, and reclaiming capacity without compromising system integrity.
Primary Diagnostic Tools: The Command-Line Arsenal
The df Command: Your First Line of Defense
The df utility delivers an immediate snapshot of filesystem utilization. Executing df -h presents all mounted volumes in human-readable format (gigabytes, megabytes), revealing total capacity, used space, available space, and percentage utilization.
df -h
For targeted analysis of the root filesystem—the most critical partition—append the mount point:
df -h /
Exclude temporary pseudo-filesystems (tmpfs, devtmpfs) for a cleaner view using the -x flag:
df -hx
This filters out volatile memory-based mounts, focusing attention on persistent storage where genuine capacity constraints emerge.
The du Command: Pinpointing Space Consumption
While df reports filesystem-level statistics, du (disk usage) traverses directory trees to quantify actual file consumption. To assess the home directory:
du -sh ~
The -s flag summarizes totals; -h ensures human-readable output. For hierarchical analysis of the entire root filesystem—requiring elevated privileges—combine du with sorting:
sudo du -h / --max-depth=1 | sort -h
This lists top-level directories by size, enabling rapid identification of oversized folders. Omit --max-depth for recursive detail, but anticipate verbose output on complex systems.
Advanced Reconnaissance: ncdu and lsblk
For interactive exploration, ncdu provides a navigable, terminal-based interface for disk analysis. After installation (sudo apt install ncdu), launch with:
ncdu /
Navigate directories with arrow keys, delete files directly, and exit with q. This tool excels at iterative investigation when space constraints demand precision.
To enumerate physical storage devices—SD cards, USB drives, external SSDs—use lsblk:
lsblk
This reveals device names, partition structures, mount points, and capacities, clarifying which volume hosts the operating system versus supplemental storage.
Uncovering Hidden Space Consumers
Log Files and System Journals
System logs accumulate silently. The /var/log directory frequently harbors gigabytes of rotated logs, debug output, or application traces. Audit its contents:
sudo du -hd1 /var/log
Trim historical journal entries with:
sudo journalctl --vacuum-time=2weeks
This retains only recent logs while discarding older records unlikely to aid future troubleshooting.
Package Cache and Orphaned Dependencies
The APT package manager retains downloaded archives in /var/cache/apt/archives. Over time, this cache consumes significant space. Purge it safely:
sudo apt clean
Remove automatically installed dependencies no longer required by any package:
sudo apt autoremove --purge
This reclaims space from obsolete libraries and kernel images without affecting active software.
The sudo Requirement: A Critical Caveat
A frequent investigative dead end arises when du reports minimal usage while df indicates near-capacity. This discrepancy often stems from permission restrictions: ordinary users cannot traverse system directories like /root, /var, or /usr/local. Always prefix disk-usage commands with sudo for comprehensive results:
sudo du -h --max-depth=1 / 2>/dev/null | sort -h
Redirecting standard error (2>/dev/null) suppresses permission-denied messages, yielding a cleaner output focused on measurable consumption.
Proactive Storage Management Strategies
Real-Time Monitoring
For continuous oversight, combine watch with df:
watch -n 10 df -h /
This refreshes the display every ten seconds, enabling observation of storage trends during intensive operations like media processing or data logging.
Identifying Large Files
Locate individual files exceeding a threshold with find:
find ~/ -type f -size +100M
Adjust the size parameter to target files of concern. Review results before deletion; large files may include legitimate media, databases, or project assets.
Filesystem Health Checks
Beyond capacity, inode exhaustion can cripple a system even with free space. Check inode utilization:
df -i
A high inode count with low block usage suggests excessive small files—common in development environments or containerized applications.
Frequently Asked Questions
What is the quickest way to check Raspberry Pi disk space?
Execute df -h / in the terminal. This displays human-readable usage statistics for the root filesystem, including total capacity, used space, available space, and percentage utilization.
Why does df show high usage while du reports low consumption?
This discrepancy typically indicates files deleted while still held open by running processes, or insufficient permissions preventing du from scanning system directories. Run du with sudo and restart services holding deleted files to resolve the mismatch.
Which directories most commonly consume Raspberry Pi storage?
Primary candidates include /var/log (system and application logs), /var/cache/apt/archives (package manager cache), /home/pi (user files and downloads), and /var/lib/docker (if using containerized workloads). Audit these first during space investigations.
How can I prevent future disk space shortages?
Implement routine maintenance: schedule apt clean and apt autoremove via cron, configure log rotation limits, monitor usage with watch df -h, and consider expanding storage via larger SD cards or external USB drives for data-intensive projects.
Is it safe to delete files in /var/log or /var/cache?
Yes, with caution. Clearing old logs via journalctl --vacuum-time or removing package cache with apt clean is safe and reversible. Avoid manually deleting active log files; instead, use system utilities designed for log management to prevent service disruption.