Managing and backing dotfiles in Linux

If you've been using Linux for a while, you've probably accumulated a collection of dotfiles that you use to customize your system. Dotfiles are configuration files that start with a period, which makes them hidden by default. These files can be anything from shell scripts to configuration files for applications like Vim or Emacs. In this article, we'll discuss how to manage your dotfiles in a way that makes it easy to back them up, sync them across multiple machines, and keep them organized. Fot our purpose I am using zsh as shell and fedora linux.

Step 1: Create a dotfiles directory

The first step is to create a directory to store all of your dotfiles. We recommend creating a directory called "dotfiles" in your home directory. You can do this with the following command:

mkdir ~/dotfiles        

Step 2: Organize your dotfiles

Now that you have a directory to store your dotfiles, it's time to start organizing them. You can create subdirectories within the "dotfiles" directory to categorize your dotfiles based on their purpose. For example, you could create a subdirectory called "zsh" for all of your Zsh-related dotfiles. Within the "zsh" directory, you can put your current dotfiles related to zsh. You can create other sub-directory like nvim, ranger etc. Lets procees with the zsh dotfiles. I asume you have your current zsh related dotfiles inside the zsh sub-directory.

For an example lets put zshenv an zshrc inside zsh subdirectory.


# zshen
export XDG_DATA_HOME=$HOME/.local/share
export XDG_CONFIG_HOME=$HOME/.config
export XDG_STATE_HOME=$HOME/.local/state
export XDG_CACHE_HOME=$HOME/.cache

export ZDOTDIR=$HOME/.config/zshv        

I assumes you have knowledge of the given variables if not you can search them up.

And our zshrc look like:

# zshenv
export HISTFILE=$XDG_STATE_HOME/zsh/zsh_histor
export LESSHISTFILE=$XDG_STATE_HOME/less/history
export PATH=$HOME/.local/bin:$PATH
HISTSIZE=10000
SAVEHIST=10000        

Now lets build a script that can automatically link this file so that we can use the same dotfiles even after distro hopping. Lests create a script.sh fiel inside the dotfiles directory:

# scrip.sh
ln-svf $PWD/zsh/zshenv $HOME/.zshenv        

Here at first it will create the soft link for our zshenv file to .zshen, lets add some more content to script.sh:

# script.sh
ln -svf $PWD/zsh/zshrc $ZDOTDIR/.zshrc        

We have this two lines defined in script.sh. Now we can just create a git repo to track the changes of this dotfiles, push them to get server whenever necessary and boom even changing our os will not affect our configuration. We can get back to our previous configuration with:

./script.sh        

And our dot files will be inside their respective path. We can extend this script a little further by adding other dotfiles, handiling exception etc.

要查看或添加评论,请登录

Dipesh Regmi的更多文章

社区洞察

其他会员也浏览了