Articles
Published · November 18, 2026

The XDG Base Directory Spec: Why Tools Put Config in ~/.config

The XDG Base Directory spec gives tools four default folders under your home directory: config in ~/.config, data, state and cache. Here is what each holds and what the spec asks of a tool.

Also in: Español中文हिन्दी

Quick answer

The XDG Base Directory spec is a convention that tells programs where to keep your files. Settings go in ~/.config, data in ~/.local/share, state in ~/.local/state and cache in ~/.cache. A tool that follows it puts its config in one predictable folder instead of adding another dotfile to your home directory.

Where the spec comes from

The spec is published by freedesktop.org. According to Wikipedia's freedesktop.org entry, the project was formerly the X Desktop Group (XDG), it produces specifications for interoperability, and it is not a formal standards body. That last point matters. Nobody enforces the spec, so each tool chooses whether to follow it.

The current text on specifications.freedesktop.org is version 0.8, published 8 May 2021.

The four user directories

The spec defines environment variables, each with a default that applies when the variable is unset or empty:

XDG_CONFIG_HOME  ->  $HOME/.config
XDG_DATA_HOME    ->  $HOME/.local/share
XDG_STATE_HOME   ->  $HOME/.local/state
XDG_CACHE_HOME   ->  $HOME/.cache

Each folder has a job:

  • Config holds settings you or the tool edit.
  • Data holds user-specific files the program works with.
  • State holds things that should persist between restarts but are, in the spec's words, not important or portable enough for the data folder. Undo history is the spec's example.
  • Cache holds files a program can rebuild.

There is also XDG_RUNTIME_DIR, with no default. It must be owned by you, with access mode 0700, and its files must not survive a reboot or a full logout and login.

Why dotfiles pile up in your home folder

A tool that hardcodes a path such as ~/.toolrc adds one more hidden entry to your home directory. After a few years, the list is long. The spec gathers those files under a small set of folders, so a backup, a sync job or a quick look finds config in one place. The spec also keeps different kinds of files apart: a cache you can delete without worry no longer sits next to the settings you would hate to lose. Shell setup has its own rules, as which zsh startup file runs when shows, and those files usually sit in the home folder anyway.

A real example: Git

Git follows the convention. Its git-config documentation lists two user-level files, $XDG_CONFIG_HOME/git/config and ~/.gitconfig. When XDG_CONFIG_HOME is not set or empty, $HOME/.config/ is used in its place.

If both files exist, Git reads both, in that order, and the last value found wins. When you write with --global, Git writes to ~/.gitconfig, unless only the XDG file exists, in which case it writes there. For the full layering with system and repository files, see where Git settings live.

Rules tools should follow

The spec is short. A few rules are worth knowing:

  • Absolute paths only. If a variable holds a relative path, the spec says to treat it as invalid and ignore it.
  • Create with `0700`. If the destination directory does not exist when a tool writes, it should try to create it with permission 0700. If the directory already exists, its permissions should not be changed.
  • System directories come second. XDG_CONFIG_DIRS defaults to /etc/xdg, and XDG_DATA_DIRS to /usr/local/share/:/usr/share/. The first directory listed is the most important, and XDG_CONFIG_HOME is more important than anything in XDG_CONFIG_DIRS, so your own settings win.
  • Skip what you cannot read. If a file in a directory is inaccessible, processing of that directory should be skipped.

In practice that means you can read a tool's settings in one folder, copy that folder to a new machine, and leave the cache and state behind. To move your config somewhere else, set XDG_CONFIG_HOME to an absolute path. Tools that follow the spec will look there.

When to use it, and when to skip it

If you write a command-line tool, following the spec is little work and keeps your users' home folders tidy. If you only use tools, there's not much to do. Check each tool's documentation to see where it looks, since some still use their own dotfile.

The spec does not say what is inside a config file. That's up to each tool, and the formats vary: INI-style files, JSON, YAML, TOML and plain key-value lines are all common. If you want to handle a few of them, a quick config edit covers the minimum keys for a terminal editor.

How Carets fits in

Config files under ~/.config are plain text, which is why they are easy to back up and compare. Carets is a native notes and code editor for iPhone, iPad and Mac. It handles Markdown, plain text and code in one place, and it applies syntax highlighting to code and config files, so a TOML or YAML file reads clearly. You can also keep your notes on dotfiles alongside the snippets, organized into files, projects and tags. Your files stay plain text, so they stay portable.

Get Carets on the App Store for iPhone, iPad and Mac.

Frequently Asked Questions

Where is XDG_CONFIG_HOME by default?

When XDG_CONFIG_HOME is unset or empty, the spec says to use $HOME/.config, so a tool that follows it keeps its settings in a folder under ~/.config.

What is the difference between XDG_DATA_HOME and XDG_STATE_HOME?

Data, under ~/.local/share by default, holds user-specific files the program works with. State, under ~/.local/state, is for things that should persist between restarts but are not important or portable enough for the data folder, such as undo history.

Do all tools follow the XDG Base Directory spec?

No. The spec comes from freedesktop.org, which is not a formal standards body, so each tool decides whether to follow it. Check a tool's own documentation to see where it looks.

Can I change where the config folder is?

Yes. Set XDG_CONFIG_HOME to an absolute path. The spec says relative paths are invalid and should be ignored.