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.
df -h /Two tools used later, ncdu and lsof, are not on a stock server. Install them now:
sudo apt update
sudo apt install -y ncdu lsofYou get a table like this:
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.
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:
sudo find /var -xdev -type f 2>/dev/null | cut -d/ -f1-3 | sort | uniq -c | sort -n | tail -n 5Each 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:
sudo du -h --max-depth=1 /var 2>/dev/null | sort -h-hprints human sizes, andsort -hunderstands them.--max-depth=1stops at one level, so the output stays short.2>/dev/nullhides 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:
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -n 8On 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:
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 -hThe 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:
sudo find /tmp/diskdemo -type f -size +10M -exec ls -lh {} +-type fmatches files only.-size +10Mmatches files larger than 10 megabytes.ls -lhprints 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.
sudo ncdu -x /varIt 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:
sudo apt-get clean
sudo apt-get autoremove -yapt-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:
journalctl --disk-usage
sudo journalctl --vacuum-size=500MThe 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:
sudo ls -lh /var/log/*.gz
sudo find /var/log -name '*.gz' -mtime +30 -deleteDo 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:
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
logrotatereads/etc/logrotate.d/. Add a file for your own app's log. - Cap the journal permanently by setting
SystemMaxUse=500Min/etc/systemd/journald.conf, thensudo 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:
rm -rf /tmp/diskdemoNext 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.
Related tutorials
- How to Schedule Jobs with Cron on LinuxYou will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.
- How to Run Your App as a systemd Service on Ubuntu 26.04You will write a systemd unit file that starts your app at boot, restarts it when it crashes, runs it as an unprivileged user, and sends its logs to the journal.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials