Using bash to automate dotfiles

后端 未结 6 1950
执笔经年
执笔经年 2021-02-06 09:21

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

相关标签:
6条回答
  • 2021-02-06 10:00

    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

    0 讨论(0)
  • 2021-02-06 10:12

    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

    0 讨论(0)
  • 2021-02-06 10:13

    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 Tooling is hard and necessary where I wrote about managing for multiple devices.

    0 讨论(0)
  • 2021-02-06 10:17

    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
    
    0 讨论(0)
  • 2021-02-06 10:18

    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:

    • Here is my dotfiles selection using dfm: vicente's dotfiles
    • Github official repository DFM site
    0 讨论(0)
  • 2021-02-06 10:24

    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.

    0 讨论(0)
提交回复
热议问题