When an Ubuntu server or desktop system runs low on primary storage, standard cleanup commands like sudo apt autoremove and sudo apt clean only remove orphaned dependencies and cached package archives. They do not reveal which installed applications, runtime libraries, or database packages are actually occupying the largest portions of your root filesystem.
Because Debian and Ubuntu manage software through the dpkg package database, the built-in dpkg-query tool can extract exact installed byte counts directly from package metadata. Combining dpkg-query with standard Unix sorting filters provides an instant, accurate inventory of disk space consumption.
Step 1: List All Packages Sorted by Size
To inspect every installed package on your Ubuntu machine sorted from smallest to largest in kilobytes, run the following pipeline in your terminal:
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n
Here is how this command pipeline operates:
dpkg-query -W: Queries the dpkg database for installed package status.-f '${Installed-Size}\t${Package}\n': Formats the output to print the installed size (in kilobytes), followed by a tab separator, followed by the package name.| sort -n: Pipes the resulting stream into the numerical sort utility to order values from lowest to highest.
Step 2: Show the Top 20 Largest Packages
Listing all several thousand packages can overwhelm your terminal buffer. To isolate only the top 20 storage-heavy packages, pipe the sorted stream through tail:
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 20
To view them in human-readable megabytes and gigabytes, you can process the size values through numfmt:
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 20 | awk '{printf "%sK\t%s\n", $1, $2}' | numfmt --from=auto --to=iec --field=1
This prints a tidy two-column readout displaying sizes like 850M or 1.4G alongside each package identifier.
Step 3: Filter Out Essential System Packages
Before purging packages, it is crucial to recognize that many large packages (such as linux-image-*, linux-modules-*, or gcc) are critical core components. If you notice outdated Linux kernels lingering on the system, remove them safely using APT rather than manual file deletion:
sudo apt purge <package-name>
If you frequently audit package storage, you can also install the dedicated debian-goodies suite, which includes the dpigs command:
sudo apt update && sudo apt install -y debian-goodies
dpigs -H -n 10
The dpigs utility automatically queries the package registry and outputs the 10 largest packages with human-readable size labels in seconds.