The XDG Base Directory Specification (commonly referred to as the XDG standard) defines where applications should store different types of user files on Linux and other Unix-like systems. It helps organize config files, data files, cache, and runtime files in a consistent and user-respecting way.

Here are the core environment variables defined by the XDG spec:


📁 1. XDG_CONFIG_HOME


📁 2. XDG_DATA_HOME


📁 3. XDG_CACHE_HOME


📁 4. XDG_STATE_HOME (optional; newer addition)


📁 5. XDG_RUNTIME_DIR


📂 Additional (for searching system-wide locations)


🧠 Why use XDG?


🛠 Tip for developers

You can write fallbacks like:

A concise XDG Base Directory cheat sheet you can keep handy:

XDG Base Directory Cheat Sheet

Env VarPurposeDefaultExample Path
XDG_CONFIG_HOMEUser-specific config files$HOME/.config$HOME/.config/myapp/settings.json
XDG_DATA_HOMEUser-specific data files (app data)$HOME/.local/share$HOME/.local/share/myapp/database.db
XDG_CACHE_HOMEUser-specific cache (rebuildable)$HOME/.cache$HOME/.cache/myapp/image_cache/
XDG_STATE_HOMEUser-specific state files (logs, etc.)$HOME/.local/state$HOME/.local/state/myapp/history.log
XDG_RUNTIME_DIRRuntime files (sockets, PID files)(set by system at login)/run/user/1000/myapp.sock

System-wide Locations


Quick-start in Shell


Quick-start in Python