Sysadmin

Linux: Free Up Disk Space with 12 Commands (SysAdmin Guide 2026)

Linux: Free Up Disk Space with 12 Commands (SysAdmin Guide 2026)

Every sysadmin‘s nightmare: a Linux server suddenly running out of disk space. This isn’t just an annoyance; it’s a direct threat to service stability and uptime. I’ve seen critical infrastructure grind to a halt over a simple No space left on device error—blocking updates, failing backups, or worse, crashing essential applications. Proactive disk space management isn’t a luxury; it’s a core competency.

This article walks you through 12 essential commands and practical techniques to identify, analyze, and free up Linux disk space, turning a potential disaster into a streamlined maintenance routine. We’ll cover diagnostic tools, package cleanup, log management, and automation, giving you a complete arsenal to tackle the root cause.

Prerequisites / Test Environment

To follow along, you’ll need SSH access to a Linux system (Ubuntu, CentOS, Debian, or derivatives) with sudo privileges. All commands were tested on Ubuntu Server 22.04 and CentOS Stream 9. Always back up critical data before running deletion operations, especially in production. Our main focus—free up Linux disk space—will guide every section.

Check Available Disk Space with df

The first step in solving a space issue is pinpointing where it lies. The df (disk free) command is your starting point. Using the -h (human-readable) flag displays disk space in an easily digestible format (GB, MB).

df -h

This output gives you an immediate overview of disk usage across all mounted filesystems. Look for usage percentages (Use%) creeping toward 100% to identify problematic filesystems.

Find Space-Hungry Directories with du

Once you’ve identified a full filesystem, the next step is finding which directories are consuming the most space. du (disk usage) is the perfect tool for this. Starting from the root of the problematic filesystem, you can trace down the hierarchy.

du -sh /* 2>/dev/null | sort -rh | head -20

This command lists the 20 largest directories starting from the root (/), sorted by size in descending order. The 2>/dev/null flag suppresses permission errors, keeping the output clean. Repeat this process by navigating into the largest directories (cd /path/to/directory) until you pinpoint the source of the space consumption.

The 10 Largest Files on the System

Often, the culprit isn’t a whole directory, but a few massive individual files. These might be unrotated logs, forgotten database dumps, or downloaded ISOs never removed. The find command is incredibly powerful for this type of search.

find / -type f -size +100M -exec ls -lh {} ; 2>/dev/null

This command searches for all files (-type f) larger than 100 MB (-size +100M) across the entire filesystem (/) and displays their details using ls -lh. Adjust the size (+100M) to fit your needs. [Read also: Hardening SSH on Linux: 10 Essential Configurations]

Find Files Older Than N Days

Temporary files, obsolete logs, or outdated backups can accumulate significantly over time. find can help you identify and even delete files based on their last modification or access date.

find /var/log -name '*.log' -mtime +30 -delete

This command finds all .log files in the /var/log directory that were modified more than 30 days ago (-mtime +30) and deletes them (-delete). Use find /tmp -type f -atime +7 -delete to remove temporary files older than 7 days from the /tmp directory. Exercise extreme caution with the -delete flag and always test it on a small subset of files first.

Clean Up Unnecessary Packages (apt/yum)

Package managers like apt (Debian/Ubuntu) or yum/dnf (CentOS/RHEL) tend to accumulate packages that are no longer required or download caches. Cleaning up these leftovers can free up Linux disk space significantly.

On Debian/Ubuntu-based systems:

sudo apt autoremove && sudo apt clean

autoremove removes automatically installed dependencies that are no longer required by any package. clean clears out the downloaded package cache (/var/cache/apt/archives).

On CentOS/RHEL-based systems:

sudo yum autoremove && sudo yum clean all
# O per DNF:
sudo dnf autoremove && sudo dnf clean all

Flush the APT Cache

As mentioned, apt clean is crucial. If you haven’t run it, the APT cache can grow uncontrollably. This command is safe and won’t affect your installed packages.

sudo apt clean

Clear Out Old Logs with journalctl

systemd-journald manages system logs on Linux. If not configured properly, the journal can consume gigabytes of space. You can manage this with journalctl.

To limit logs to a specific timeframe (e.g., 7 days):

sudo journalctl --vacuum-time=7d

To limit logs to a maximum size (e.g., 500 MB):

sudo journalctl --vacuum-size=500M

These commands will remove older logs or reduce the overall size of the journal, helping you free up Linux disk space in the /var/log partition.

Find Duplicate Files with fdupes

Duplicate files are another common cause of wasted space. Tools like fdupes can identify and optionally remove them. You’ll need to install it first, for example, with sudo apt install fdupes.

fdupes -r /percorso/da/scansionare

The -r flag recursively scans directories. fdupes will present you with a list of duplicate files and prompt you to choose which ones to keep. Be very careful when using this tool, especially within system directories.

Compress Logs with gzip

Instead of deleting logs, sometimes it’s enough to compress them to save space while keeping them available for future analysis. gzip is the standard tool for this.

gzip /var/log/vecchio.log

This will create /var/log/vecchio.log.gz, drastically reducing the size of the original file. To decompress it later, use gunzip /var/log/vecchio.log.gz.

Clean Up Unused Docker Images

If you use Docker, unused images, containers, and volumes can pile up quickly, consuming gigabytes. Docker provides a powerful cleanup command.

docker system prune -a

This command removes all stopped containers, unused networks, dangling images, and, with the -a flag, all images not associated with a container. It’s extremely effective for freeing up Linux disk space in Docker environments. For more details, check the official Docker Prune documentation.

Automation: Weekly Cleanup Script

Manual maintenance is prone to human error and forgetfulness. Automating disk space cleanup is the best approach. You can create a bash script and schedule it with cron.

Create a file /etc/cron.weekly/cleanup_disk.sh (or cron.daily for more frequent cleanup) with the following content:

#!/bin/bash

# Pulizia pacchetti APT
sudo apt autoremove -y
sudo apt clean

# Pulizia cache thumbnail
rm -rf ~/.cache/thumbnails/*

# Pulizia log journald (es. 30 giorni)
sudo journalctl --vacuum-time=30d

# Pulizia /tmp (file più vecchi di 7 giorni)
find /tmp -type f -atime +7 -delete

# Pulizia Docker (se usato)
docker system prune -af

logger "Pulizia disco completata su $(hostname)"

Make the script executable (sudo chmod +x /etc/cron.weekly/cleanup_disk.sh) and cron will run it automatically. Customize the paths and timeframes based on your needs. Remember that automating cleanup requires careful planning to avoid deleting essential data.

Common Pitfalls and Troubleshooting

  • Deleting Essential System Files: The most critical mistake. Never delete files or directories unless you are absolutely certain of their function. When in doubt, move files to a temporary directory and only delete them after verifying the system operates correctly.
  • Ignoring Permissions: Many cleanup commands require sudo privileges. If a command fails, double-check that you have the necessary permissions.
  • Forgetting External Volumes: If you have external disks or NFS mounts attached, df will include them. Focus your cleanup efforts on the problematic local disk.
  • Unrotated Logs: If logs continue to grow unchecked, verify your logrotate or systemd-journald configuration to ensure logs are being compressed and purged regularly.

Conclusion: Key Takeaways

Freeing up Linux disk space is a critical maintenance task that shouldn’t be ignored. It’s not just about reacting to emergencies, but implementing a proactive strategy. Here are the key takeaways:

  1. Monitor Continuously: Use monitoring tools (like Prometheus/Grafana or Nagios) to keep an eye on disk usage and get alerts before a problem occurs.
  2. Automate Cleanup: Leverage cron to automate the removal of temporary files, stale logs, and unnecessary packages. This reduces manual overhead and prevents unwanted buildup.
  3. Understand Your Data Structure: Before deleting anything, use du and find to understand exactly what is consuming space and why. In-depth knowledge will save you from costly mistakes.
  4. Verify Configurations: Ensure logrotate and systemd-journald settings are optimized for your workload to prevent uncontrolled log growth.

By adopting these practices, you’ll keep your Linux servers lean, efficient, and safe from unpleasant disk space surprises.

Share this article:

Written by

Rosario Giordano

Rosario Giordano is a system administrator and IT consultant specializing in cybersecurity and cloud, with over 20 years of experience managing enterprise Linux infrastructures. His areas of expertise include SSH hardening, Kubernetes platforms, PostgreSQL databases, VMware/ Proxmox virtualization, and compliance with NIS2 and ISO 27001 security frameworks