Skip to main content

Clean home directory using XDG base directory specification

A comprehensive guide to implementing the XDG Base Directory Specification for organising configuration files and maintaining a clean home directory on Linux systems.

The proliferation of dotfiles in a Unix-like home directory can quickly become unwieldy. Configuration files, cache data, and application state scattered across numerous hidden files and directories make backup, maintenance, and system migrations challenging. The XDG Base Directory Specification offers a standardised solution to this problem by defining specific locations for different types of user data.

Understanding XDG base directory specification

The XDG Base Directory Specification, developed as part of the freedesktop.org project, establishes a set of standard locations for storing user-specific application data. This specification defines four main directories, each serving a distinct purpose in organising configuration and data files.

The primary directories defined by the specification include:

  • Configuration directory for user-specific configuration files
  • Data directory for user-specific data files
  • Cache directory for non-essential data files
  • State directory for state files that should persist between application restarts but are not configuration files

Core environment variables

The XDG specification relies on environment variables to define these standard locations. The default paths are designed to work without explicit configuration, but users can customise them to suit their needs. The essential environment variables are:

  • XDG_CONFIG_HOME (defaulting to ~/.config)
  • XDG_DATA_HOME (defaulting to ~/.local/share)
  • XDG_CACHE_HOME (defaulting to ~/.cache)
  • XDG_STATE_HOME (defaulting to ~/.local/state)

These environment variables can be set in your shell's initialisation file, such as .bashrc or .zshrc:

export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"

Application support and implementation

While many modern applications support the XDG specification natively, some older or less maintained applications continue to use the home directory directly. For these applications, various workarounds exist to maintain a clean home directory while ensuring functionality.

The most straightforward approach involves using environment variables specific to each application. For example, many tools can be configured to use XDG-compliant directories by setting appropriate environment variables:

# For Python virtual environments
export WORKON_HOME="$XDG_DATA_HOME/virtualenvs"

# For PostgreSQL
export PSQLRC="$XDG_CONFIG_HOME/pg/psqlrc"
export PSQL_HISTORY="$XDG_STATE_HOME/psql_history"

# For less
export LESSKEY="$XDG_CONFIG_HOME/less/lesskey"
export LESSHISTFILE="$XDG_STATE_HOME/less/history"

For applications without direct environment variable support, symbolic links provide an alternative solution. These links can redirect traditional dotfile locations to their XDG-compliant counterparts:

ln -s "$XDG_CONFIG_HOME/vim" "$HOME/.vim"

Implementing XDG compliance

Implementing XDG compliance across your system requires a systematic approach. Begin by identifying applications that store data in your home directory and researching their XDG support status. For applications with native support, configuration typically involves setting the appropriate environment variables and moving existing configuration files to their new locations.

The vim text editor serves as an excellent example of implementing XDG compliance:

  1. Create a configuration directory and move your vimrc file:
mkdir -p "$XDG_CONFIG_HOME/vim"
mv "$HOME/.vimrc" "$XDG_CONFIG_HOME/vim/vimrc"
  1. Add the following configuration to specify the new locations:
export VIMINIT='let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" | source $MYVIMRC'

Common applications and their XDG implementations

Below are examples of XDG configurations for popular command-line tools:

Bash
Move .bash_history to XDG locations with export HISTFILE="$XDG_STATE_HOME/bash/history"
Python
Configure pip with export PIP_CONFIG_FILE="$XDG_CONFIG_HOME/pip/pip.conf" and for startup modules export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
Git
Set git config --global core.configFile "$XDG_CONFIG_HOME/git/config" or use the appropriate environment variable
Wget
Create a configuration file at $XDG_CONFIG_HOME/wget/wgetrc and add alias alias wget='wget --hsts-file="$XDG_DATA_HOME/wget/wget-hsts"'

Configuration management

Managing configurations becomes more structured with XDG compliance. The centralised nature of XDG directories simplifies backup procedures and system migrations. Consider using version control systems like Git to track configuration changes:

cd "$XDG_CONFIG_HOME"
git init
git add .
git commit -m "Initial configuration backup"

This approach enables easy configuration synchronisation across multiple systems and provides a history of changes for troubleshooting purposes.

Tip

When migrating to a new system, you can simply clone your configuration repository and set up the appropriate environment variables. This makes system setup much faster and more reliable compared to manually copying dotfiles.

Advantages of XDG implementation

Implementing the XDG Base Directory Specification offers several significant benefits beyond simple organisation:

  • Improved discoverability: Configuration files become more discoverable and manageable when stored in standard locations
  • Simplified backups: System backups become more straightforward as important user data is concentrated in known directories
  • Enhanced security: Separation of different types of data allows for appropriate permissions for each category
  • Easier system migration: Moving to a new system becomes simpler with well-organised configuration files
  • Reduced clutter: Home directory remains clean and manageable, with fewer hidden files and directories

Best practices and considerations

When implementing XDG compliance, consider these best practices:

  • Maintain documentation of your configuration changes and environment variables
  • Create a bootstrap script that sets up the necessary directory structure and symbolic links:
#!/bin/bash
# Create XDG Base Directories
mkdir -p "${XDG_CONFIG_HOME:=$HOME/.config}"
mkdir -p "${XDG_CACHE_HOME:=$HOME/.cache}"
mkdir -p "${XDG_DATA_HOME:=$HOME/.local/share}"
mkdir -p "${XDG_STATE_HOME:=$HOME/.local/state}"
  • Regularly maintain your XDG directories to prevent accumulation of obsolete configurations and cache data
  • Implement periodic cleaning of cache directories with scripts:
#!/bin/bash
# Clean cache older than 30 days
find "$XDG_CACHE_HOME" -type f -atime +30 -delete
  • Review configuration files for outdated entries during system maintenance
  • Use symbolic links only when necessary, preferring environment variables when available

Dealing with applications that don't support XDG

For applications that don't natively support the XDG specification:

  1. Check if the application has configuration options to specify alternative paths
  2. Look for environment variables that can redirect configuration locations
  3. Use wrapper scripts or aliases to pass appropriate parameters
  4. Consider using symbolic links as a last resort
  5. Contribute to open-source projects by submitting patches for XDG support

Conclusion

The XDG Base Directory Specification provides a robust framework for organising user-specific application data. By implementing XDG compliance across your system, you create a more maintainable and organised environment. While the initial setup requires some effort, the long-term benefits in terms of system organisation, backup simplicity, and maintenance efficiency make it a worthwhile investment for any Linux user.

Consider starting with a few applications and gradually expanding XDG compliance across your system. Focus initially on frequently used applications where the benefits will be most noticeable. As you become more comfortable with the specification, expand your implementation to cover more of your system's applications and tools.

Further resources