Dotfiles management
Dotfiles Management
Managing dotfiles across multiple machines for your user's ${HOME}
can be very tedious and inefficient when it is done manually.
So, for the purpose of not having to copy,move or symlink all dotfiles individually, we can leverage a very old command line tool offered by the GNU project, namely stow
.
Essentially, stow
can be seen as a symlink manager that will create and/or adopt files in a given directory and symlink these to their counterparts in another filesystem location that follows a well organized directory tree structure read by the stow command.
GNU Stow is a symlink farm manager which takes distinct packages of software and/or data located in separate directories on the filesystem, and makes them appear to be installed in the same place.
For more information on stow
's functionality and usage please refer to its official documentation.
Getting started
To get started, first you will have to create a directory in a preferred filesystem location and organize it's directory structure in a way so that stow
can create the symlinked files/directories in the right locations.
In the example further down you will discover why the directory has to follow a certain pattern.
For this example I have cloned a git repository named dotfiles
into my ${HOME}'
where the directory tree looks like this:
dotfiles/
├── bash
│ ├── .bash_aliases
│ ├── .bash_functs
│ ├── .bash_logout
│ └── .bashrc
├── i3
│ └── .config
│ └── i3
│ └── config
The first level directories of the ~/dotfiles/
are so called packages in the context of stow
. What ever it contains will be symlinked one directory above the current working directory of the stow
command.
For example:
Let's say your ${PWD}
(current working directory) is ~/dotfiles
. When you run stow bash
, stow will look for the bash
package directory, which in this case is ~/dotfiles/bash
and symlink all its contents to ../${PWD}
, namely your home directory.
This will create the following files in your home directory:
~/
[ ... ]
├── .bash_aliases -> dotfiles/bash/.bash_aliases
├── .bash_functs -> dotfiles/bash/.bash_functs
├── .bash_logout -> dotfiles/bash/.bash_logout
├── .bashrc -> dotfiles/bash/.bashrc
[ ... ]
If you want all packages in ~/dotfiles
to be symlinked, simply run stow
without specifying a package name.
Handling existing files
In some cases it could be that some of the files that you want to symlink already exist. This is very likely for a file like .bashrc
. However, there is a hacky way of handling this kind of situation.
For the purpose of this example, we assume that the file .bashrc
already exists and stow
has failed to create a symlink. The file in question exists just like above in ~/dotfiles/bash
so you can run the command below.
Warning
git reset --hard
will overwrite the contents of the pre-existing conflict file(s) on your system.
cd ~/dotfiles
stow bash --adopt
git reset --hard
If the above commands ran successfully, the files found in your home directory should be the same as in your dotfiles git repository.