Save disk space by excluding useless files with dpkg

Most packages contain files that you don’t need: for example translations in languages that you don’t understand, or documentation that you don’t read. Wouldn’t it be nice if you could get rid of them and save a few megabytes? Good news: since dpkg 1.15.8 you can!

dpkg has two options --path-include=glob-pattern and --path-exclude=glob-pattern that control what files are installed or not. The pattern work the same than what you’re used to on the shell (see the glob(7) manual page).

Passing those options on the command-line would be impractical, so the best way to use them is to put them in a file in /etc/dpkg/dpkg.cfg.d/. Beware, the order of the options does matter: when a file matches several options, the last one makes the decision.

A typical usage is to first exclude a directory and then to re-include parts of that directory that you want to keep. For example if you want to drop gettext translations and translated manual pages except French, you could put this in /etc/dpkg/dpkg.cfg.d/excludes:

# Drop locales except French
path-exclude=/usr/share/locale/*
path-include=/usr/share/locale/fr/*
path-include=/usr/share/locale/locale.alias

# Drop translated manual pages except French
path-exclude=/usr/share/man/*
path-include=/usr/share/man/man[1-9]/*
path-include=/usr/share/man/fr*/*

Note that the files will vanish progressively every time that a package is upgraded. If you want to save space immediately, you have to reinstall the packages present in your system. aptitude reinstall or apt-get --reinstall install might help. In theory with aptitude you can even do aptitude reinstall ~i but it tends to not work because one package is not available (either because it was installed manually or because the installed version has been superseded by a newer version on the mirror).

Found it useful? Click here to see how you can encourage me to provide more articles like this one.

Additional Resources

Get the Debian Administrator's Handbook

After a successful liberation campaign, the Debian Administrator's Handbook is now freely available. If you appreciate my articles and what I do for Debian, check out the book and grab a copy.

Comments

  1. I guess that the example with locale removals is just that; a example. But for the sake of the matter: the package localepurge does the locale removal nicely.

  2. @Brother
    Well the warning on the localepurge page is scaring :

    Please note, that this tool is a hack which is *not* integrated with Debian’s package management system and therefore is not for the faint of heart. This program interferes with the Debian package management and does provoke strange, but usually harmless, behaviour of programs related with apt/dpkg like dpkg-repack, reportbug, etc. Responsibility for its usage and possible breakage of your system therefore lies in the sysadmin’s (your) hands“.

    • I know localepurge. It is a hack, the files are first installed and then again removed. I think this feature should deprecate localepurge… or maybe localepurge can become a tool that generates those dpkg configuration files for the user.

      That said, even I did not put a scary warning in this article, there’s a scary warning in the dpkg manual page. This feature should be used with care as you can break your system easily and sometimes files that you thought useless were actually required.

      • I put the scary warning in first place as a measure to shy away users who
        wouldn’t be able to help themselves. Obviously there never appeared any
        real issue for anybody over the years, but i prefer to keep up the fear of
        wreaking havoc. ;)

        In fact, localepurge should be made completely obsolete. In first place, it would
        have been nice if i wouldn’t had to waste any time in creating it, but back then
        my hard disks were too small, and dpkg was not up to the task yet. Now it is!

        I noted the wishlist bug #603587 you sent today, and it is only now that i
        became aware of this great new dpkg exclusion/inclusion feature. Looks
        like the days of localepurge might finally be counted. :)

  3. Problem is that on my Lucid machine that I have been running since pre hardy and all locales installed only take up 54mb ;/

    Nice idea in practice can’t think of any other files I could safely remove/ignore.

  4. Can you do this at install time? Seems like the best way, is at install time for the OS.

    • @Nate, there’s no official integration within debian-installer, but there are enough hooks in d-i that you can use to create those files if you wish to customize the installation process (with preseeding of debconf questions).

  5. Pedro Tales says:

    thanks for that tip

    only hope that the canonical design team have some time to spend on such idea. As dpkg is one of the master package it should be nice to have a gui to set its parameters via check boxes to include/exclude what user want/need. I still cant understand why the ubuntu installer, for example, is so monolitic and closed; then synaptic/apt-get/dpkg are missing modern friendly gui to be used by “Humans”.

  6. That is really useful, thanks.

    On my OpenVZ server /vz/private/*/usr/share/locale/^en* (“^en*” does match anything except “en*” in zsh) makes up for more than 1.3GB!
    It would be nice to trigger removing those files without reinstalling the packages, e.g. by looking at files being matched by the patterns in the exclude file and if a package can be found for this file (à la “dpkg -S”), remove it.
    Is something like this planned?

    • @Daniel, I don’t know anyone working on this. I filed a wishlist bug against dpkg (#603700) to record your wish but I don’t know whether someone will implement it (and even less when that might be done).

  7. Thanks, reading all these tips, I making me desire to go back to Debian in a more daily basis.
    Thanks and keep the good work.

  8. Interesting, but I doubt I would use it. Most of my hard drive space is used by data and not packages. Space only matters for me on embedded systems, and I wouldn’t use Debian for those.

  9. Doesn’t the package “localepurge” ( Reclaim disk space removing unneeded localizations) do this automatically?

    Anyway, good feature to script with dpkg.

  10. Very good idea for dpkg.

  11. Excellent. I’m always looking for ways to save space on my little old laptop. And then why not declutter my PC and eventually the work server!

  12. Michael Öman says:

    Hi! I wonder perhaps little odd question, but I am new in Linux and all those distros. Does this also works in Ubuntu 10.10?

    Thanks
    Michael

  13. Idea is nice, localepurge is also nice, but there’s also bleachbit, which removes much more not needed files, including caches and different temporary files. If someone want’s to free some space, I suggest taking a look on bleachbit.