XDG refers to a set of standards and specifications created by freedesktop.org to enhance interoperability and consistency across Linux desktop environments. These standards simplify system behavior for users and developers by defining file locations, application launching, MIME types, and other key functionality.

XDG Base Directory Specification

The XDG Base Directory Specification defines standardized paths for storing configuration, data, and cache files in a user’s home directory, ensuring better organization and portability.

  • Key Directories:

    • XDG_CONFIG_HOME: Stores configuration files (default: ~/.config).
    • XDG_DATA_HOME: Holds application data files (default: ~/.local/share).
    • XDG_CACHE_HOME: Used for non-essential cached files (default: ~/.cache).
    • XDG_RUNTIME_DIR: Stores runtime data (default: /run/user/<UID>).
  • Defaults: If an environment variable (e.g., XDG_CONFIG_HOME) is unset, a fallback location is used.

  • Customization: Users can override default paths by setting environment variables:

    export XDG_CONFIG_HOME=~/custom_config
    export XDG_DATA_HOME=~/custom_data
  • Directory Localization: XDG also defines localized directories (e.g., ~/Documents, ~/Downloads) in ~/.config/user-dirs.dirs.

  • Useful Commands:

    • View directory paths:
      echo $XDG_CONFIG_HOME $XDG_DATA_HOME $XDG_CACHE_HOME
    • Update localized directories:
      xdg-user-dirs-update

XDG and Application Integration

Applications adhering to XDG standards use its directory specification for better file management. For example:

  • Config files (XDG_CONFIG_HOME): Application settings (~/.config/appname/config).
  • Cache files (XDG_CACHE_HOME): Temporary data (~/.cache/appname/cache).
  • Data files (XDG_DATA_HOME): Resources like themes or icons (~/.local/share/appname/data). Benefits:
  • Separation of Concerns: Configuration, data, and cache are logically separated.
  • Portability: Simplifies migration between systems.

XDG Utilities

XDG utilities help manage desktop behavior and follow XDG specifications.

  • xdg-open: Opens a file or URL using the default application.
    xdg-open file.txt
    xdg-open https://example.com
  • xdg-user-dir: Returns the path of a specific user directory.
    xdg-user-dir DOCUMENTS
  • xdg-mime: Queries or changes file type associations.
    xdg-mime query filetype file.pdf
    xdg-mime default evince.desktop application/pdf
  • xdg-desktop-menu: Installs or removes desktop menu entries for applications.
  • xdg-settings: Manages system-wide settings like default browsers.
    xdg-settings get default-web-browser
    xdg-settings set default-web-browser firefox.desktop

Tips for Working with XDG

  • To see all configured directories:
    env | grep XDG
  • Reset localized directories to defaults:
    xdg-user-dirs-update --force
  • Combine with find or grep to locate files:
    find $XDG_CONFIG_HOME -name "config.json"
    grep -r "keyword" $XDG_DATA_HOME

Applications Beyond the Base Directory

  • File Managers: Nautilus and Dolphin rely on XDG to determine default folders.
  • Flatpak: Respects XDG-defined paths for sandboxed file access (e.g., xdg-documents).

XDG is a critical part of Linux desktop environments, promoting consistency across distributions and streamlining user and developer workflows.