Linux: Dotfiler (Dotfiles on Git)

I keep my dotfiles backed up on GitHub. Until yesterday, I did so by directly adding dotfiles to my repos from my home folder (~). This was silly, and I knew this was silly: it prevents you from creating any other git repositories in your home folder.

The thing was, the proper way to go about versioning and backing up dotfiles is to create a separate directory  (say ~/.dotfiles) and symlink them to ~ from there. I hadn’t found a script or utility or something that let me do that easily. There are utilities that symlink files in .dotfiles, but I didn’t find anything that would then recurse through folders in .dotfiles. (If anyone reads this post and finds one, for goodness sake please let me know.) This posed a problem, since I have quite a few configuration files in .config and other dotfolders, and I want to preserve the file structure.

Yesterday, I decided to sit down and see if I could hack up a quick script that would not only symlink files from ~/.dotfiles, but also from ~/.dotfiles/<insertfoldernamehere>. DISCLAIMER: THIS IS NOT EXTENSIVELY TESTED. IF YOU BLOW UP YOUR COMPUTER, I AM NOT RESPONSIBLE. I assume that you know some basic bash (and probably more than me), and verifying this stuff before running it is your responsibility. If any of this gets you into trouble, I am not responsible.

First I had to get my dotfiles from ~ into ~.dotfiles. I ridiculously ran this script:

dotfiles=$(git ls-files)
for dotfile in $dotfiles; do
cp –parents $dotfile ~/.dotfiles

This was ridiculous because I could simply have done a

git pull origin master

from ~/.dotfiles and it would have populated it with my existing GitHub dotfiles. I also had to clean up my existing dotfiles in ~, which were still versioned with git. I simply replaced the above cp line with rm:

dotfiles=$(git ls-files)
for dotfile in $dotfiles; do
rm $dotfile

However, in my script later, I do interactively force creation of symlinks, so this step isn’t very necessary.

Now, I need a command that would finds every regular file, including ones inside folders, in ~/.dotfiles:

find ~/.dotfiles/ -type f

I then iterate through each file, create its parent folders if necessary, and symlink. Finally, I clean up any dangling symlinks in ~ with:

find -L ~ -type l -delete

The entire dotfiler script is on GitHub, and is still very much a work in progress. I am 100% certain that the thing is not efficient, not clean, and not perfect. There must be a better way to do this. However, I don’t have zillions of dotfiles, and it works for me thus far. It’s dirty, it’s messy, but it works. I’m still looking for a better way, though. If you know of one, please let me know!

Tagged , , ,

2 thoughts on “Linux: Dotfiler (Dotfiles on Git)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: