Skip to main content

Linux and servers · Beginner · 10 min

How to Find and Free Up Disk Space on a Linux Server

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS

You will find which folders and files fill a Linux disk with df and du, free space safely from logs and packages, and track down space held by deleted files.

On this page

A full disk stops databases from writing, makes package installs fail and can take a server down at an awkward hour. The fix is usually quick once you know where the space went. In this tutorial you will check how full each disk is, drill down folder by folder to the culprit, clear the safe things first, and handle the odd case where space does not come back after you delete a file.

Work from the top down: disk, folder, file. Delete nothing until you know what it is.

Step 1 · See how full the disks are

df reports space per file system. The -h flag prints sizes in human units.

Bash
df -h /

Two tools used later, ncdu and lsof, are not on a stock server. Install them now:

Bash
sudo apt update
sudo apt install -y ncdu lsof

You get a table like this:

Text
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   21G  3.2G  87% /

The row for / is your main disk. Pay attention to Use%. Once it passes ninety percent, act.

A disk can also run out of inodes, the records that hold file metadata, while space remains. Millions of tiny files cause this. The symptom is No space left on device with space still free.

Bash
df -i /

If IUse% is close to full, the problem is the number of files, not their size. Count the files per folder instead of the bytes. This lists the five folders directly under /var that hold the most files:

Bash
sudo find /var -xdev -type f 2>/dev/null | cut -d/ -f1-3 | sort | uniq -c | sort -n | tail -n 5

Each line is a file count followed by a folder. Go into the folder with the highest count and repeat the command one level deeper, with -f1-4, until you find the culprit.

Check it worked

You know which mount point is full. On a server with extra volumes, run df -h with no path and look at every row.

Step 2 · Find the folders that are using the space

du adds up the size of everything under a path. This command lists the first level of folders under /var, largest last:

Bash
sudo du -h --max-depth=1 /var 2>/dev/null | sort -h
  • -h prints human sizes, and sort -h understands them.
  • --max-depth=1 stops at one level, so the output stays short.
  • 2>/dev/null hides permission and vanished-file errors.

Take the biggest folder, then run the same command inside it, and repeat. Start at / with -x, which stays on one file system and does not wander into other mounts:

Bash
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -n 8

On servers the usual suspects are /var/log (logs), /var/lib (databases, Docker), /home (uploads and backups) and /var/cache (package files).

To prove the method here, create a folder with a few files of different sizes and rank them:

Bash
mkdir -p /tmp/diskdemo/logs /tmp/diskdemo/uploads
dd if=/dev/zero of=/tmp/diskdemo/logs/app.log bs=1M count=30 status=none
dd if=/dev/zero of=/tmp/diskdemo/uploads/video.mp4 bs=1M count=80 status=none
dd if=/dev/zero of=/tmp/diskdemo/uploads/photo.jpg bs=1M count=5 status=none
du -h --max-depth=1 /tmp/diskdemo | sort -h

The last line printed is 116M /tmp/diskdemo, and above it uploads (86M) and logs (31M).

Check it worked

Each run narrows the search. When you reach a folder full of individual files, stop.

Step 3 · Find the large files

Search by size instead of by folder:

Bash
sudo find /tmp/diskdemo -type f -size +10M -exec ls -lh {} +
  • -type f matches files only.
  • -size +10M matches files larger than 10 megabytes.
  • ls -lh prints their sizes.

On the whole disk, use sudo find / -xdev -type f -size +500M 2>/dev/null. The -xdev option stays on one file system.

For an interactive view, ncdu is a terminal disk browser. Run it on a folder, move with the arrow keys, press d to delete the highlighted item and q to quit.

Bash
sudo ncdu -x /var

It scans first, then sorts every folder by size. The -x flag keeps it on one file system.

Step 4 · Free the safe things first

Delete only what you can name and rebuild or lose without harm.

Package cache. Downloaded .deb files stay in /var/cache/apt/archives. Clear them:

Bash
sudo apt-get clean
sudo apt-get autoremove -y

apt-get autoremove removes packages that were installed as dependencies and are no longer needed. Drop the -y to read the list before you confirm.

Journal logs. systemd keeps its journal under /var/log/journal. See how much it uses and shrink it to a size you choose:

Bash
journalctl --disk-usage
sudo journalctl --vacuum-size=500M

The first prints Archived and active journals take up 1.2G in the file system. The second deletes the oldest archived entries until the journal is 500 MB or less. --vacuum-time=14d keeps the last 14 days instead.

Old log files. Rotated logs end in .gz or a number, such as syslog.2.gz. Remove the ones you no longer need:

Bash
sudo ls -lh /var/log/*.gz
sudo find /var/log -name '*.gz' -mtime +30 -delete

Do not delete or empty an active log file with rm; truncate it instead, so the program writing to it keeps working: sudo truncate -s 0 /var/log/myapp.log.

Docker. If the server runs Docker, docker system df shows what images and volumes use, and docker image prune removes dangling images. Do not run docker system prune --volumes until you have read what it will remove.

Check it worked

Run df -h / again. Avail should have grown by roughly what you removed.

Troubleshooting

If you deleted a large file and df shows no change, a running program still holds it open. Linux frees the space only when the last open handle closes. List deleted files that are still open:

Bash
rm -f /tmp/diskdemo/held.log
dd if=/dev/zero of=/tmp/diskdemo/held.log bs=1M count=20 status=none
exec 3< /tmp/diskdemo/held.log
rm /tmp/diskdemo/held.log
lsof -nP +L1 | grep held.log
exec 3<&-

The line shows the command and PID holding the file, and ends with /tmp/diskdemo/held.log (deleted). In practice you will find a service. Restart it (sudo systemctl restart myapp) and the space returns. If you cannot restart it, truncating through /proc/<pid>/fd/<number> frees the space, but a restart is the cleaner fix.

Step 5 · Stop it from happening again

  • Make sure logs rotate. Ubuntu's logrotate reads /etc/logrotate.d/. Add a file for your own app's log.
  • Cap the journal permanently by setting SystemMaxUse=500M in /etc/systemd/journald.conf, then sudo systemctl restart systemd-journald.
  • Watch usage. A simple cron job that mails or logs df -h / once a day gives you a warning before it is an outage.

Clean up the practice folder:

Bash
rm -rf /tmp/diskdemo

Next steps

  • Schedule the clean-up so you do not have to remember: /tutorials/schedule-jobs-with-cron.
  • Move a growing app's logs to the journal: /tutorials/run-an-app-as-a-systemd-service.
  • Check who owns the files you are removing: /tutorials/linux-file-permissions-chmod-chown.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.