Critical Notice
Administering disk space on a server is a critical task. If your server runs out of storage space, it can crash, leading to service interruptions and potential data loss. Regular monitoring and maintenance are essential for stable server operations.
๐ Overview
Disk space administration involves several key tasks that ensure your server maintains optimal performance and availability. This guide covers the following essential aspects:
- Monitoring disk usage: Regular checks to track storage consumption
- Removing unnecessary packages: Cleaning up unused software dependencies
- Clearing local repository: Removing cached package files
- Managing system logs: Removing old log files to free up space
- Identifying storage-intensive directories: Finding areas consuming excessive storage
๐ Step 1: Check Disk Space
1Run the disk free command to view current storage usage:
Command Explanation
The -h flag means human-readable format, which displays sizes in
KB, MB, and GB rather than raw bytes.
Sample Output:
Important Line to Monitor
The line with Mounted on "/" is the most critical. In the example above:
- Size: 23 GB (total capacity)
- Used: 7.4 GB
- Available: 15 GB
- Use%: 33%
๐ Disk Usage Visualization
Current Disk Usage: 33%
Warning Threshold: 60%
Critical Threshold: 85%
๐ Recommended Monitoring Schedule
Best Practice: Run this command every 2-3 weeks and maintain a history of your server's storage usage.
- Take a screenshot of the
df -houtput every two weeks - Store screenshots with dates to build a historical record
- Compare results over time to identify trends
- When usage reaches approximately 60%, begin cleanup procedures
๐งน Step 2: Clean Unused Packages
2Remove packages that are no longer needed and clear the local repository:
What This Command Does
autoremove removes packages that were automatically installed as dependencies for
other software but are no longer needed by any installed programs.
What This Command Does
clean clears out the local repository of retrieved package files. These are cached
.deb files that remain after package installation and can safely be removed.
Combined Command (Run Both at Once):
๐ Expected Results
Before cleanup:
After running both commands:
โ Immediate Impact
You should see an immediate reduction in disk usage. In this example, 100 MB was freed. The actual amount freed depends on how many unused packages and cached files were present.
Verify the changes:
๐ Step 3: Clean System Log Files
3Remove old system log files to free up additional space:
Command Explanation
This command uses the journalctl utility to manage system logs:
- journalctl: System logging utility for Linux servers
- --vacuum-time=1days: Removes archived log files older than 1 day
You can adjust the time period based on your needs (e.g., 2days, 1week, 1month).
Sample Output:
๐ Cumulative Results
Disk Space Recovery Progress
| Stage | Action Taken | Disk Used | Space Freed |
|---|---|---|---|
| Initial | Starting point | 7.4 GB | - |
| After Package Cleanup | apt autoremove & clean | 7.3 GB | ~100 MB |
| After Log Cleanup | journalctl vacuum | 7.0 GB | ~400 MB Total |
๐ Step 4: Analyze Directory Sizes
4Identify directories consuming the most storage space:
First, navigate to the root directory:
Then run the disk usage analysis command:
Command Breakdown
- du: Disk usage command - estimates file and directory space usage
- -a: Include all files (including hidden files)
- -h: Human-readable format (KB, MB, GB)
- --max-depth=1: Limit analysis to current directory and immediate subdirectories
- | sort -h: Pipe output to sort command, ordered by size in human-readable format
Sample Output (sorted by size, largest at bottom):
๐ Deep Dive into Large Directories
Based on the output above, /usr (3.5 GB) and /var (1.3 GB) are the largest directories. Let's investigate further:
Change to the /var directory:
Run the same du command to see subdirectories:
Sample Output:
Here we can see that /var/lib (1.1 GB) is the largest subdirectory, followed by /var/log (150 MB).
๐ Analyzing Web Server Directories
Navigate to the web directory:
Analyze web directory space usage:
Sample Output:
The website directory example.com is using 87 MB of storage.
โ ๏ธ Critical Safety Warning
DO NOT Delete Files in System Directories
Exercise extreme caution when exploring system directories. Deleting the wrong files can render your server inoperable.
Forbidden Directory: /lib
Let's examine why the /lib directory is critical:
โ ABSOLUTE PROHIBITION
- NEVER work inside the /lib directory
- NEVER delete files or directories in /lib
- NEVER modify anything in system directories unless you are absolutely certain of the consequences
The /lib directory contains essential system libraries. Deleting or modifying files here WILL break your server!
โ Safe Directories for Cleanup
Approved Areas for Manual Cleanup
You may safely check and clean the following directories:
1. Home Directory
Look for unnecessary downloaded files, old backups, or temporary files you no longer need.
2. /var Directory
Check for old log files that haven't been automatically cleaned, or unnecessary cached data.
3. /tmp Directory
Temporary files can often be safely removed, though the system usually cleans this automatically.
Golden Rule
If you're unsure about a file or directory, DO NOT DELETE IT. Research first or consult with experienced administrators. When in doubt, leave it alone.
๐ง The du Command: A Powerful Troubleshooting Tool
The du command is invaluable for troubleshooting disk space issues. It allows you to:
- Navigate systematically: Start from root (/) and drill down into specific directories
- Identify storage hogs: Quickly locate directories consuming excessive space
- Target cleanup efforts: Focus your attention on the largest consumers of storage
- Track changes over time: Compare directory sizes before and after cleanup operations
Workflow Example:
Identify largest dirs
Drill deeper
Find old logs
Remove if appropriate
๐ Complete Maintenance Checklist
Weekly/Bi-weekly Tasks
- Monitor disk usage:
df -h
- Take a screenshot for historical tracking
- If usage exceeds 60%, proceed with cleanup:
Cleanup Procedure (When Needed)
- Remove unused packages and clean cache:
sudo apt autoremove && sudo apt clean
- Clean old system logs:
sudo journalctl --vacuum-time=1days
- Verify results:
df -h
- If more space is needed, analyze directories:
cd /sudo du -ah --max-depth=1 | sort -h
- Navigate to large directories and investigate further
- Remove only files you're certain are safe to delete
๐ Decision Matrix for Disk Space Management
| Disk Usage | Status | Action Required | Urgency |
|---|---|---|---|
| 0-50% | โ Healthy | Regular monitoring only | Low |
| 51-60% | โ Normal | Continue monitoring, prepare for cleanup | Low |
| 61-75% | โ Warning | Perform cleanup procedures | Medium |
| 76-85% | โ High Usage | Immediate cleanup + directory analysis | High |
| 86-95% | ๐ด Critical | Emergency cleanup + consider expansion | Critical |
| 96-100% | ๐ด Emergency | Immediate action - server crash imminent | Emergency |
๐ก Best Practices Summary
Key Recommendations
- Regular monitoring: Check disk usage every 2-3 weeks
- Historical tracking: Maintain screenshots to identify trends
- Proactive cleanup: Begin cleanup when usage reaches 60%
- Automated tasks: Set up cron jobs for automatic package cleanup
- Log rotation: Ensure system logs are properly rotated and archived
- Safety first: Never delete system files or directories you don't understand
- Research first: When in doubt, investigate before taking action
- Backup important data: Always maintain backups before major cleanups
- Document changes: Keep records of what was cleaned and when
๐ฏ Conclusion
Proper disk space administration is essential for maintaining a healthy, stable server environment.
By following this guide and implementing regular monitoring and cleanup procedures, you can prevent
disk space issues before they become critical problems. The du command is your most
valuable tool for identifying storage issues and targeting your cleanup efforts effectively.
Remember: Prevention through regular monitoring is far easier than recovery from a crashed server due to full disk space.