'Using bash to automate dotfiles

I want to create my own automated dotfiles folder. (I'll be using git to use version control on my dotfiles, but that's irrelevant for the question)

What i simply want is to symbolically link all the files and folders in ~/dotfiles to my home folder. Being not good at all with bash I can't do this. Please help me with this.

I would also appreciate the following features if possible.

  • Folders are only shallowly linked
  • My files could be in the dotfiles folder without the actual dot in the file-name (like ~/dotfiles/vimrc rather than ~/dotfiles/.vimrc)
  • It should be able to ignore some files, like my .git file which are stored in the same folder

Of course if you already know a service providing this, that is at least as good as providing some do-myself commands. Note how I specifically want it to be bash or something that most likely exists on all unix machines (so i guess commands using g++ are fine).



Solution 1:[1]

I am not sure if I'm getting the question right, but if you looking for symlinks of dir-content, try

for f in `ls -1 .dotfiles`
do
   ln -s .dotfiles/$f ~/$f
done

maybe that already does the trick

Solution 2:[2]

For the sake of managing dotfiles, I really like Zach Holman's approach. I've made the same thing with my dotfiles which you can find it here :)

https://github.com/rhacker/dotFiles

Solution 3:[3]

Maybe you are looking for a dotfiles manager, I will recommend you to check DFM (dotfiles manager). DFM addresses the problem you have in a very clean way:

Solution 4:[4]

Atlassian has a tutorial on using a git work tree instead of symlinks. The approach uses:

  1. a bare git repository in a side folder (such as $HOME/.cfg or $HOME/dotfiles), and
  2. a config bash alias to execute git commands that manage the configuration files.

For instance, you can run config status to check which files have been modified, config checkout to get the files in the repository and config commit to update the repository. It requires only git and the bash.

Solution 5:[5]

I was also looking for some way to set up a new machine in a minimal number of steps, after spending some time I found that almost all the developers use git to store and share these files and symlinks to sync them.

Well, symlinks works, but it isn’t the best way to sync your local files to the git repository. There is a much better solution to this, written by people at Atlassian – https://www.atlassian.com/git/tutorials/dotfiles.

So, to git bare repository is the best and most elegant way to sync your files with your remote copy create a bash script to automate installation and set up.

Managing dotfiles

The trick to managing these dotfiles is by creating a bare git repository. If you are starting from scratch and have not tracked your dotfiles before, make a bare repository in the $HOME directory.

git init --bare $HOME/.dotfiles

Just to make it easier to use we’ll alias this to dotfiles which we will use instead of regular git to interact with our dotfiles repository.

alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

Now we are good to track our dotfiles using the dotfiles command, some of the examples are:

# to check the version history 
dotfiles log

# to check the status of the tracked and untracked files 
dotfiles status

# to add a file for tracking
dotfiles commit .vimrc -m ".vimrc added"

# push new files or changes to the github
dotfiles push origin main

Use Cases and Advantages of dotfiles

This method of managing and sharing has various advantages some of them are listed below.

Easy setup

Set up of a new machine can be a time consuming task, but with this method we can to use our personalized configs in under a minute. We just need to clone the repository and source the .bashrc or .zshrc file.

git clone --bare https://github.com/<username>/dotfiles.git $HOME/.dotfiles && source ~/.zshrc

Versioned dotfiles

Best for experimenting with new configurations and keep the change history (Basically all the pros of using git)

# to check the version history 
dotfiles log

Share on Multiple devices

Share the same configs of multiple devices with minimal changes using branch, create a branch for your new machine, example:-

# Create configurations specific to your aws machines
dotfiles checkout -b aws

Profiles for dotfiles

Create configs based on your environment using branch, create a branch and configure according to you work env.

# Manage multiple profiles - check out to the work profile 
dotfiles checkout work

I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jan Galinski
Solution 2 Dzung Nguyen
Solution 3 Vicente Bolea
Solution 4 Jaime
Solution 5