DEV Community

ShellProven
ShellProven

Posted on Originally published at shellprovenai.github.io

Disk full? Find what's eating your space with df, du and sort (Linux)

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (GNU coreutils 9.7); all outputs below are copied from that run, not written by hand.

A step-by-step way to find what's filling a Linux disk: check the filesystem with df -h, drill down with du -sh and du --max-depth piped into sort -h, list the biggest files with find -size, empty a log safely with truncate, and solve the classic mystery where df says the disk is full but du can't find the files (a deleted file that's still open).

The problem: No space left on device. Your app stops writing, your database refuses to start, and you need to know what's filling the disk, fast.

Step 1: which filesystem is full? df -h

Start with df. It reports space per filesystem, and -h prints sizes in human units.

$ df -h /lab
Filesystem      Size  Used Avail Use% Mounted on
overlay          59G   21G   36G  37% /
Enter fullscreen mode Exit fullscreen mode

In a container you see the overlay filesystem. On a server you'd look for the line with a high use percentage, usually the one mounted on slash, or on slash var. df answers which disk is full. It can't tell you which files. That's the job of du.

Step 2: which folder? du -sh

du -sh gives one total per argument. With a star, that's one line per folder here.

$ du -sh *
93M app
205M    backups
3.1M    uploads
Enter fullscreen mode Exit fullscreen mode

Backups is clearly the biggest. But sorting by eye doesn't scale, so let's let the computer sort.

Step 3: drill down with --max-depth and sort -h

--max-depth=2 shows totals two levels deep, and sort -h understands human sizes, so ten megabytes sorts before one gigabyte.

$ du -h --max-depth=2 . | sort -h
3.1M    ./uploads
25M ./app/cache
69M ./app/logs
93M ./app
205M    ./backups
300M    .
Enter fullscreen mode Exit fullscreen mode

The biggest are at the bottom. Plain sort, without -h, would compare the text and put a hundred megabytes before nine megabytes. To see the top offenders, files included, add -a and reverse the sort.

$ du -ah . | sort -rh | head -6
300M    .
205M    ./backups
104M    ./backups/db-2026-11.sql.gz
100M    ./backups/db-2026-10.sql.gz
93M ./app
69M ./app/logs
Enter fullscreen mode Exit fullscreen mode

Step 4: list the big files with find

If you already know what's big for you, find can list every file above a size, here fifty megabytes, wherever it is.

$ find . -type f -size +50M -exec ls -lh {} +
-rw-r--r-- 1 root root  60M Nov 21 19:39 ./app/logs/app.log.1
-rw-r--r-- 1 root root 100M Nov 21 19:39 ./backups/db-2026-10.sql.gz
-rw-r--r-- 1 root root 104M Nov 21 19:39 ./backups/db-2026-11.sql.gz
Enter fullscreen mode Exit fullscreen mode

Two database dumps and a rotated log. These are the usual suspects on a real server: old backups, logs and caches.

Step 5: free space safely

To empty a log that a program may still be writing, don't delete it. truncate -s 0 cuts it to zero bytes, and the file stays in place.

$ truncate -s 0 app/logs/app.log.1 && du -sh app
33M app
Enter fullscreen mode Exit fullscreen mode

Be careful with this on files you might need: the content is gone for good. For old backups, move them somewhere else before you delete anything.

The mystery: df says full, du disagrees

One more case that confuses everyone. Let's create a big temporary file.

$ fallocate -l 200M big.tmp && du -sh .
440M    .
Enter fullscreen mode Exit fullscreen mode

Now a process opens it and keeps it open. Here that's just sleep, reading from the file in the background.

$ sleep 600 < big.tmp &
Enter fullscreen mode Exit fullscreen mode

And someone deletes the file to free space.

$ rm big.tmp && du -sh .
240M    .
Enter fullscreen mode Exit fullscreen mode

The file is gone from the folder, so du doesn't count it. But the process still holds it open, so the space is not freed, and df still shows it as used. To find such files, look for open file handles marked deleted in slash proc.

$ find /proc/*/fd -lname '*(deleted)' -printf '%p -> %l\n' 2>/dev/null
/proc/120/fd/0 -> /lab/srv/big.tmp (deleted)
Enter fullscreen mode Exit fullscreen mode

The number in the path is the process I D. Stopping or restarting that process releases the space.

$ kill %1
Enter fullscreen mode Exit fullscreen mode

The trap, in one paragraph

I deleted a 200 MB file, but df still counts the space as used. What is the likely cause? The space comes back only when that process closes the file, or is restarted.

Quick check

1. After du -h, why sort -h and not plain sort?

  • sort -h is faster
  • sort -h understands sizes like 9M and 100M
  • sort -h sorts by folder name

Answer
sort -h understands sizes like 9M and 100M: Plain sort compares text, so a hundred megabytes would land before nine megabytes.

2. I deleted a 200 MB file, but df still counts the space as used. What is the likely cause?

  • The disk is damaged
  • A process still has the deleted file open
  • du skips hidden files

Answer
A process still has the deleted file open: The space comes back only when that process closes the file, or is restarted.

Recap

df -h tells you which filesystem is full. du -sh and du --max-depth, piped into sort -h, tell you which folder. find -size lists the big files, truncate -s 0 empties a live log, and if df and du disagree, look for deleted files that are still open.

Source and reproducible setup


This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

Top comments (0)