3

Is there a way to compute the size of all files that are not managed by the package manager? For example all stuff that is in /opt, /var/, /usr/local, etc.

I would like to find out how much disk space is used by installed packages and compare that to the disk space that is used by unmanaged files.

Update: My question is different from What is taking up so much space on my disk, beside the filesystem?

I know that on apt/dpkg based distributions like Debian and Ubuntu the package manager, e.g dpkg keeps track of all files that are installed with it. We can use tools like dpkg-query to get information abotu these packages.

For example I tried to get the size of all installed packages with this command:

dpkg-query -W --showformat='${Installed-Size}\n' | \
    sort -k1,1n |awk '{s+=$1} END {print s}'

but I believe this is not correct, because the calculated sum is very close to the total occupied space in / and I have several GB of files stored under /opt that should not count towards this result.

Essentially what I am looking for is some tools that can distinguish between files installed by the package manager, and all the other files which are on the root file system but are not managed by the package manager.

When my disk is starting to fill up I would like to know, if I:

a) Installed too many packages or b) There are too many other files on the file system that take up space.

So I would like to get something like the following information about my root file system:

56GB of 60GB are used on '/':

  * 32GB are used by installed packages
  * 24GB are used by other files

When I use a tool like baobab, it does not distinguish between package manager installed files and any other file.

lanoxx
  • 1,229

1 Answers1

0

There is no reliable way to do so, since packages may download files while installing that dpkg wouldn't be aware of. The package would clean up if uninstalled, so they're, in a sense, managed, but dpkg doesn't track them. And while dpkg knows the sizes from the package, the files could, of course, have changed after installation.

Packages can have files in /opt (Google Chrome, for example), /var/ (way too many examples to list, just do dpkg -S /var), /usr/local (SSL certificates are to be installed using a directory in /usr/local, for one example).

Reliably, the only directories that you can exclude are /home, /cdrom, /mnt, /media (and /lost+found, but that's an artefact of the filesystem, not standard).

For me, the command you tried, and the following one are in reasonable agreement, and differ significantly with the actual filesystem usage (the list of files from a package is in /var/lib/dpkg/info/<package>.list):

$ (cat /var/lib/dpkg/info/*.list | sudo xargs -d '\n' stat -c '%s' | awk '{c+=$1}END{print int(c/1024)}') 2>/dev/null
5572166

Compare:

$ dpkg-query -W --showformat='${Installed-Size}\n' | awk '{c+=$1}END{print c}'
5462957
$ df /
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       18446076 6744528  10741500  39% /

Personally, I just check the sizes of /var/cache and /home.

muru
  • 197,895
  • 55
  • 485
  • 740