XDG Base Directory Specification

created onMarch 29, 2023
last modified onDecember 24, 2023

XDG stands for X Desktop Group, a project to work on interoperability for free-software desktop environments for the X Window System (X11) and Wayland on Linux and other Unix-like operating systems. It eventually evolved into freedesktop.org. freedesktop.org produces specifications for interoperability, but it is not a formal standards body. Projects are not required to implement the specifications and there is no certification.

The XDG Base Directory Specification specifies a set of environment variables and configuration files that specify locations that user programs should use to write specific data types, such as configuration files, cache files, runtime files and user data like documents, music, images and so on. The specification also defines default values for unset environment variables and unset configuration values.

Linux distributions usually provide a system-wide XDG configuration in . The system-wide configuration can be overwritten on a per-user basis.

benefits of using XDG directories

To me, the XGD Base Directory Specification is useful in two ways:

  • using XDG directories keeps the home directory less cluttered and more organized. You don’t end up with dozens of dotfiles and config directories like directly in your home dir.
  • Organizing your data files in respect of data type helps running your backups faster and keeping your backup files smaller. I don’t find it particularly useful for my backups to include stuff like image thumbnails or my web browsers cache. In case of a data loss, such data is automatically reconstructed on the fly (i.e. image thumbnails) or not of much importance (i.e. a web browser’s cache).

Those two points only apply as far as programs adhere to the XDG Base Directory Specification. I have compiled lists of programs that adhere and do not adhere to the XDG spec.

configuring the XDG directories

There are a bunch of programs to configure XDG and read its configuration (see below programs to manage XDG Base Directories). Personally, I think those programs are a bit of an overkill and I do well without them. The XDG Base Dirs can be configured by simply exporting the respecting environment vars in your . The image below illustrates the concept:

XDG Base Dirs configuration overview

In the upper left hand in the image there is the home directory tree of the (fictional) user alice plus a directory within , which alice uses for her runtime data that is to be cleaned up periodically.

In the upper right hand is an excerpt of alice’s that contains some XDG Base Directories config vars.

On the lower right hand of the image is the system-wide XDG config file overwritten by the user specific config file in alice’s home dir.

The most straight-forward way to configure the XDG Base Directories:

  • export the respective env vars in your .
  • additionally, if you use locations that get removed during a shutdown/reboot of your machine (i.e. in ), create those locations before you export the env var.
  • edit and disable with .

That’s it.

Here is an example of a section in :

### XDG base dir spec export XDG_CACHE_HOME=${HOME}/.cache export XDG_CONFIG_HOME=${HOME}/.config export XDG_STATE_HOME=${HOME}/.state export XDG_DATA_HOME=${HOME}/files/data export XDG_RUNTIME_DIR=/tmp/rudolf/runtime mkdir -p ${XDG_RUNTIME_DIR} && chmod 0700 ${XDG_RUNTIME_DIR} export XDG_DOWNLOAD_DIR=${HOME}/files/download export XDG_DESKTOP_DIR=${HOME}/files/desktop export XDG_DOCUMENTS_DIR=${HOME}/files/docs export XDG_TEMPLATES_DIR=${HOME}/files/templates export XDG_PUBLICSHARE_DIR=${HOME}/files/public export XDG_MUSIC_DIR=${HOME}/files//media export XDG_PICTURES_DIR=${HOME}/files/media export XDG_VIDEOS_DIR=${HOME}/files/media

For the list of env vars for XDG Base Directories and env vars for user specific data dirs see the sections below.

environment variables

XDG base directories

All environment variables start with . For most environment variables the specification defines a default that should be used if the variable is not set or empty. The XDG Base Directory spec specifies the following directories:

config var default usage
XDG_CONFIG_HOME $HOME/.config user-specific configuration of application programs, i.e. text processors and spreadsheets like libre office, graphic programs like GIMP.
XDG_DATA_HOME $HOME/.local/share user-specific data files. Unfortunately, the XDG Base Directories spec does not specify what kind of data.
XDG_CACHE_HOME $HOME/.cache user-specific non-essential (cached) data. Data that speeds up operations but is not essential for functioning of the program, i.e. a web browser’s cache.
XDG_STATE_HOME $HOME/.local/state user-specific state data; actions history (i.e. command history, recently used files) and application state (i.e. view layout, open files)
XDG_RUNTIME_DIR user-specific non-essential runtime files and other file objects such as sockets and named pipes. Data for program communication and synchronization purposes. It must be owned by the user and its Unix access mode MUST be 0700
XDG_CONFIG_DIRS /etc/xdg preference-ordered set of base directories to search for configuration files in addition to the $XDG_CONFIG_HOME base directory, with the directories separated with a colon ‘:’.
$HOME/.local/bin user-specific executable files

XDG_RUNTIME_DIR

XDG Base Directory Specification states the following requirements for :

  • The directory must be owned by the user, with read and write access limited to the user. That is, the Unix access mode must be 0700.
  • Files in must not survive reboot or a full logout/login cycle.
  • must be on a local file system that is not shared with any other system.
  • must be located in a fully featured file system that supports AF_UNIX sockets, symbolic links, hard links, proper permissions, file locking, sparse files, memory mapping, file change notifications, and a reliable hard link count. The file system should not impose any restriction on the file name character set.

It is not clear to me what a full logout/login cycle is. I suspect the meaning full logout/login cycle here is a reboot. The spec states about this:

" The lifetime of the directory MUST be bound to the user being logged in. It MUST be created when the user first logs in and if the user fully logs out the directory MUST be removed. If the user logs in more than once he should get pointed to the same directory, and it is mandatory that the directory continues to exist from his first login to his last logout on the system, and not removed in between. Files in the directory MUST not survive reboot or a full logout/login cycle. "
XDG Base Directory Specification, Environment variables

Files in may be subject to periodic clean-up. Modification of file access time timestamp at least once every 6 hours of monotonic time or setting the ‘sticky’ bit on the file prevent the file from being subject to periodic clean-up.

The XDG Base Directory Specification does not specify a default directory for :

" If $XDG_RUNTIME_DIR is not set applications should fall back to a replacement directory with similar capabilities and print a warning message. "
XDG Base Directory Specification, Environment variables

user-specific executables

The XDG Base Directory Specification expects user-specific executable files to be located in . There is no configuration value to specify the location for user-specific executables. All what the spec states about this is:

" There is a single base directory relative to which user-specific executable files may be written. [...] User-specific executable files may be stored in $HOME/.local/bin. Distributions should ensure this directory shows up in the UNIX $PATH environment variable, at an appropriate place. "

env vars for user specific data dirs

The env vars for data dirs in the user’s home dir are not specified in the XDG Base Directory spec. The system-wide config file lists the env vars, but without the suffix These env vars are, in alphabetical order:

config var
XDG_DESKTOP
XDG_DOCUMENTS
XDG_DOWNLOAD
XDG_MUSIC
XDG_PICTURES
XDG_PUBLICSHARE
XDG_TEMPLATES
XDG_VIDEOS
Noteably, the vars in for the data dirs in the user’s home dir do not start with . This is a bit confusing. Note, that the env vars for the data dirs in the user’s home (defined in ) also start with , i.e. .

config files

system-wide config files in /etc/xdg

config file usage
user-dirs.conf Enables/disables update with . Specifies the file encoding for directory names.
user-dirs.locale Created on the first run of as a kind of marker that other programs can check to find out if the locale has changed.
user-dirs.defaults Default locations for specific data, i.e., template files, downloads, music, video.

user specific config file

The system-wide config file can be overwritten with a user specific config file . This file has to be located in the dir configured with or .

config file usage
user-dirs.dirs contains the locations for specific data, i.e., template files, downloads, music, video.

programs to manage XDG Base Directories

To get an overview of the files and programs involved, run which produces an output like shown below (programs for other topics like xdg-screensaver are omitted in the output):

man -k xdg user-dirs.conf (5) - configuration for xdg-user-dirs-update user-dirs.defaults (5) - default settings for XDG user dirs user-dirs.dirs (5) - settings for XDG user dirs ... xdg-user-dir (1) - Find an XDG user dir xdg-user-dirs-update (1) - Update XDG user dir configuration

xdg-user-dirs-update

Any configured directories that point to non-existing locations are reset by pointing them to the users home directory. It is assumed that this typically happens when the users removed the directory, because they don’t want to use it anymore.

What the latter means, is that if a directory configured in no longer exists, the new value for the config var is . I.e., if the directory configured for downloads is missing, this results in downloads being dumped into the root of your home dir. This is probably not what you want. Most programs that take the XDG base dir env vars into account happily (re-)create configured but non-existent directories. Which, by the way, is the mode of operation if you run . The mode of operation based on whether the option is given is quite counter-intuitive.
only creates the specified directories. It does not edit or other startup-files to include the corresponding env vars.

tips for working with XDG Base dirs

  • I do not use the locations for music (what’s the point of having your MP3-collection in your home dir?), videos, pictures, and so on. Thus, I set the respecting env vars inside to move those dirs out of sight. Programs almost never write to those dirs anyway.
  • Some Programs write data into that is better located in , or . From time to time, I check the dirs in to see what got dumped in there and check if I can configure the programs to write to locations like and so on.

Here is the XDG section in my :

### XDG base dir spec export XDG_CACHE_HOME=${HOME}/.cache export XDG_CONFIG_HOME=${HOME}/.config export XDG_STATE_HOME=${HOME}/.state export XDG_DATA_HOME=${HOME}/files/xdg/data runtime_dir="/idoru/ram/rudolf/runtime" mkdir -p ${runtime_dir} && chmod 0700 ${runtime_dir} && export XDG_RUNTIME_DIR=${runtime_dir} export XDG_DOWNLOAD_DIR=${HOME}/files/download export XDG_DESKTOP_DIR=${HOME}/files/xdg/desktop export XDG_DOCUMENTS_DIR=${HOME}/files/xdg/docs export XDG_TEMPLATES_DIR=${HOME}/files/xdg/templates export XDG_PUBLICSHARE_DIR=${HOME}/files/xdg/public export XDG_MUSIC_DIR=${HOME}/files/xdg/media export XDG_PICTURES_DIR=${HOME}/files/xdg/media export XDG_VIDEOS_DIR=${HOME}/files/xdg/media

wrapup

The naming inconsistency between and (why not user-dirs.conf?), the default value for user-specific executables without a definition for the corresponding env var and the complete absence of all env vars for the user’s data file locations in the XDG Base Directories Specification are quite annoying. Nevertheless, the spec is quite useful and it’s application is straight forward once you have wrapped your head around the spec. For the latter, I hope this article is useful.

reference

freedesktop.org, Specifications

freedesktop.org, XDG Base Directory Specification
x