Disk Usage and Archives

Introduction

Full disks break deploys and databases. df and du show where space went; tar packages release directories for upload to servers. This chapter covers inspection commands and the archive workflow Java/Node teams use before scp or CI deploy.

Prerequisites

Check Filesystem Space

bash
# Human-readable summary of mounted filesystems
df -h
 
# Focus on root mount
df -h /

Watch Use% near 100%—writes fail, MySQL can crash, logs stop appending.

Inode exhaustion (rare but nasty):

bash
df -i

Many tiny files can fill inodes while space looks OK.

Find Large Directories and Files

bash
# Size of each item in current directory
du -sh *
 
# Home directory total
du -sh ~
 
# Top-level /var breakdown (may need sudo)
sudo du -sh /var/* 2>/dev/null | sort -hr | head

Drill down:

bash
# Largest under /var/log
sudo du -sh /var/log/* 2>/dev/null | sort -hr | head

Find huge files:

bash
sudo find / -xdev -type f -size +100M 2>/dev/null | head

-xdev stays on same filesystem—avoids crossing mounts.

Common Space Hogs on Servers

LocationOften contains
/var/logsyslog, nginx, app logs
/var/libdatabases (MySQL, Docker)
~/.gradle/cachesGradle dependencies
~/.m2/repositoryMaven cache
/tmptemporary uploads
Old releases under /optprevious JAR versions

Rotate or compress logs; truncate only when you understand impact.

Mounts and /etc/fstab (Awareness)

Disks attach to directories via mount:

bash
# Currently mounted filesystems
mount | column -t

/etc/fstab lists mounts at boot—edit carefully with sudo. Cloud VPS often give one root disk; extra volumes mount at /data.

LVM extends volumes on enterprise servers—concept only here.

tar Archives

Create compressed tarball (common for deploy bundles):

bash
# Create deploy directory sample
mkdir -p ~/release/myapp/{bin,config}
echo "v1" > ~/release/myapp/config/version.txt
 
# Create .tar.gz: c=create z=gzip f=file v=verbose
tar -czvf myapp-release.tar.gz -C ~/release myapp
 
# List contents without extracting
tar -tzvf myapp-release.tar.gz

Extract:

bash
# Extract to current directory
tar -xzvf myapp-release.tar.gz
 
# Extract to target path
mkdir -p /opt/myapp
sudo tar -xzvf myapp-release.tar.gz -C /opt

Flags:

FlagMeaning
cCreate archive
xExtract
zgzip compression
vVerbose list
fFile name follows
CChange directory before add/extract

zip and unzip

bash
# Install
sudo apt install -y zip unzip
 
# Create zip
zip -r archive.zip myfolder/
 
# Extract
unzip archive.zip

tar.gz is more common on Linux servers; zip appears when exchanging with Windows.

Deploy Workflow Sketch

On build machine or CI:

bash
# Package built app
tar -czvf myapp-1.2.0.tar.gz -C build/libs myapp-1.2.0.jar config/

Upload:

bash
scp myapp-1.2.0.tar.gz deploy@203.0.113.10:/opt/myapp/

On server:

bash
cd /opt/myapp
tar -xzvf myapp-1.2.0.tar.gz
sudo systemctl restart myapp

CI/CD pipelines automate the same steps with SSH or agents.

Clean Up Safely

bash
# Remove old release archives after verify
rm ~/myapp-release.tar.gz
 
# Clear package cache (Ubuntu)
sudo apt clean
 
# Old logs — rotate with logrotate instead of blind rm
sudo journalctl --vacuum-time=7d

Warning

rm -rf on wrong path deletes production data. Use du and ls to confirm targets. Keep backups before mass delete.

Mini Example: Pre-Deploy Disk Check

bash
# Script-friendly checks
df -h /
du -sh /opt/myapp
du -sh /var/log

If / is above 85% used, clean before uploading a large JAR.

FAQ

df vs du?

df reports filesystem blocks; du sums file sizes—can differ slightly due to metadata and open deleted files.

Delete file but space not freed?

Process still holds file open—restart the service or find PID with lsof (advanced).

tar without z?

.tar only (no gzip)—faster create, larger size. .tar.gz standard for releases.

Exclude files from tar?

tar --exclude='*.log' -czvf ...

What comes next?

Networking basics.