Back to the homepage

Keeping your $HOME clean: the XDG Base Directory specification

Cluttering of your $HOME directory by third party programs is the norm with a standard linux distribution usage. Try to execute ls -A | wc -l in your $HOME folder. If you never took steps to de-clutter your $HOME, chances are that your file count will be relatively high. This is because every program you use creates its own folder under your $HOME directory and uses it to store all the files it needs for its execution.

Even though making some order through this maze of directories may seem like a useless aesthetic non-problem, I (and other people) think that there is more to it. For example, while setting up a backup system with borgBackup [1], I wanted to save only configuration files, while excluding cache files or other unnecessary files in order to save disk space. However this is hard to implement with a standard cluttered $HOME folder, since for each program you have to understand which files are configuration, which logs, which cache, etc.

That's where the XDG Base Directory specification [2] comes in. This specification lists a set of directories and default locations, assigning a specific purpose to each one of them. Here is the full official specification [3] if you are interested in the details. The main directories defined in there are:

One may ask, what is the difference between DATA and STATE? DATA is generic information that is not tied to the specific machine (e.g. fonts, downloaded files, etc.), while STATE is for information related to the program execution on this specific machine (e.g. logs).

The XDG specification is very well done in my opinion, however the majority of programs do not follow it by default. Often times you have to set some environment variable to force the app to behave properly; this process is different for each application and can be very tedious. Thankfully there are a couple of resources that can help us out:

If you are developing an application, consider following the XDG Base Directory specification, as it makes things easier for your users.

After using the xdg-ninja and doing a bit of manual work, I managed to reduce my $HOME file count from 75 to 16. That's an improvement :)

References

  1. https://github.com/borgbackup/borg
  2. https://xdgbasedirectoryspecification.com
  3. https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  4. https://github.com/b3nj5m1n/xdg-ninja
  5. https://wiki.archlinux.org/title/XDG_Base_Directory
Back to the homepage