Jorisvandijk.com

Living on Linux

Managing dotfiles

Note: I've rethought this solution and came up with a better one. You can read about it here.

I have a new way of managing my dotfiles. I've been doing this for some time, but I have recently improved the script that does all the magic for me, so I thought I'd share.

For those that do not know, dotfiles are the configuration files used on your Linux system. They are usually denoted with a dot/period in front of them, which makes them invisible. Because of this character these configuration files are knows as dotfiles. Mine can be found here.

Manual steps

You'll need to first create a repository where you store your dotfiles. To do so, I created a directory in my home folder cleverly named ".dotfiles". In this folder I placed all the files and directories I wanted to back up. By this I mean I cut and pasted the original files here, moving them from their correct location. It looks like this.

My dotfiles

As you can see, this is a git repository. This is something you'd need to do as well. After placing the original file(s) and folder structure in this directory, you'll need to push it to its own Git repository. Make sure the directory structure is identical to the structure in your $HOME directory, as this is very important.

Once set up, you'll need to run the script below, which will create symbolic links from your repository directory (the one I named .dotfiles) to the right location in your $HOME folder.

The script

#!/usr/bin/env bash

# Dotfiles 2.4
# Manage dotfiles with a bash script
# Dependencies: none
#
# By Joris van Dijk | Jorisvandijk.com
# Licensed under the GNU General Public License v3.0

# Edit the locations:
REPO=$HOME/.dotfiles
MYHOME=$HOME

# --- DO NOT EDIT BELOW THIS LINE --

# Warning
clear
printf "\n\n\033[0;31m!!! Running this script will mess with configuration \
files on your system, which cannot be undone. !!!\033[0m

This script should be run right after installing a new system.

Using this script:\n
1. Edit the \"REPO\" and \"MYHOME\" values on lines 11 and 12 in this script \
if the following values are wrong:\e[32m
Location of dotfiles repo: $REPO
Location of \$HOME folder: $MYHOME\033[0m
2. Run script.
3. Remove files and directories that showed up in the Error List from \
\"$HOME\".
4. Run script again until no errors remain in Error List\n\n"


read -p "Are you sure you want to continue? [Y/y]" -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi

clear

# Symlinking
printf "\nSymlinking \"$REPO\" to \"$MYHOME\"...\n\n"
printf "\033[0;31m--- [ Error List ] ---\033[0m\n"

for x in $REPO/* $REPO/.*; do
case "${x##*/}" in
.|..|.git) continue # Skip these entries
esac
ln -sf "$x" "$MYHOME"
done

printf "\033[0;31m--- [ End Error List ] ---\033[0m"
# User warning
printf "\n\n\e[32mNOTE: Don't forget to take a look at the file for Code, \
found in $MYHOME and manually move the stuff in the $MYHOME/root folder \
to the root directory. \033[0m"

An up to date version of this script can be found in my dotfiles.

This script explains what it does when you run it, like so:

Dotfile script

Edit the paths so they are correct, then run the script (again)`.

Dotfile script 2

Fix any errors like the ones in the screenshot until none remain. Your system is set up again and when you decide to start over on a new set up all you need to do is:

  1. Install the new system.
  2. Clone your repo.
  3. Use the script.

And all your backed up dotfiles will be restored! Please note that the "NOTE" at the end of the script is there purely for my purposes and can be safely deleted from the script.

Bonus

To be honest, the entire script can be toned down to:

REPO=$HOME/.dotfiles

for x in $REPO/* $REPO/.*; do
case "${x##*/}" in
.|..|.git) continue # Skip these entries
esac
ln -sf "$x" "$HOME"
done

Which is a 7 line dotfile solution script. Enjoy!