Using bash to automate dotfiles
Asked Answered
R

7

7

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).

Reactive answered 8/3, 2011 at 20:35 Comment(2)
Would the links be in the ~/dotfiles dir, or would you move the actual dotfiles into that directory, and put symlinks in the original location?Minima
The second. originals in ~/dotfiles and links in ~Reactive
A
8

Give this a try:

ln -s ~/dotfiles/* ~

There shouldn't be any need for a loop. Of course, you can use find if you need something recursive.

Edit:

To make the destination files hidden:

for f in ~/dotfiles/*
do
    ln -s "$f" "$HOME/.${f##*/}"
done
Adrenaline answered 9/3, 2011 at 1:31 Comment(2)
That is good, it ignores the hidden files (like .git) aswell :). However could you make it so the files put in the home folder become hidden (this is starting with a .). For example it should link ~/dotfiles/vimrc to ~/.vimrc. Is that possible? If it is you will get correct answer. :)Reactive
Thanks! Given the knowledge you gave me, I've created my own github repo that does what I've described above plus backuping. Feel free to try it out! :) linkReactive
B
0

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

Babyblueeyes answered 8/3, 2011 at 20:40 Comment(2)
I get it to work quite well. Only I would like to be able to ignore files. Is there something like if $f == '.git' then continue? Preferably I would write a list of ignored files and folders. ThanksReactive
ls is completely unnecessary: for f in .dotfiles/* (then ln -s "$f" "$HOME/$f"). Also, you need to prefix an @ to the username so they're automatically notified of replies. @Tarrasch: if [[ $f = '.git' ]]; then continue; fi or [[ $f = '.git' ]] && continueAdrenaline
E
0

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

Embrocation answered 25/11, 2012 at 7:20 Comment(2)
What I don't like about this solution is that it depends on ruby being installed. On the other hand I dislike coding in bash due to hard to remember syntax. So depending on ruby (or other scripting languages) makes a reasonable trade off.Reactive
@Reactive if you are working with ruby, there is no problem with this. Otherwise, it depends on your need :) Also, there are some other cool tools in ruby as well. So it's gonna be a good way to install ruby :)Embrocation
M
0

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:

Martica answered 13/4, 2015 at 15:59 Comment(0)
Z
0

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.

Zwart answered 16/10, 2017 at 12:25 Comment(0)
C
0

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.

Cleek answered 21/1, 2021 at 12:14 Comment(0)
F
0

You could also use dotfile_manager. It allows you to set up a custom path for every symlink you want to create. So with this, you can easily ignore some files and add the . as a prefix to other files.

In your case you would setup the config file dotfile_manager.yaml to contain

symlinks:
  - vimrc: ~/.vimrc

Full disclosure: I am the author of this package and use it every day.

Footpoundal answered 21/8, 2022 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.