DEV Community

ujwal dhakal
ujwal dhakal

Posted on

Easily Manage Dot Files (Config Files)

Whenever I migrated from an old laptop to a new laptop or re-installed the whole OS, I always ended up copying all the config files(Ide configuration, bash history, app profiles, etc) in a hard disk.

I either ended up copying all unnecessary configs(by zipping all) from the home folder or if I pick particular configs, I keep on missing things.

image

Even if I manage to zip all those things, storing them was another big challenge for me. Storing data comes with a cost as one would need to preserve it by storing locally or in any cloud. Updating the configs is even harder as I have to manually repeat all these steps.

So I started using Git to track my config files, so I created a Private Repo where I could push my all config files.

At first, I created a bash script with the name sync. sh inside directory dotfiles

sync.sh

#
#!/bin/bash
declare -a filesToSync=(".bashrc" ".bash_history" ".zshrc" ".zsh_history" ".gitignore_global" ".gitconfig")
## now loop through the above array
for i in "${filesToSync[@]}"
do
   cp ~/$i ~/dotfiles/$i
   # or do whatever with individual element of the array
done
declare -a foldersToSync=(".config/Terminator",".config/JetBrains") 
## now loop through the above array
for i in "${foldersToSync[@]}"
do
  cp -r ~/$i ~/dotfiles
   ## or do whatever with individual element of the array
done
Enter fullscreen mode Exit fullscreen mode

So after running bash sync.sh the script would copy all my local config files and folders into the directory into the folder dotfiles.

Now all I need to do is run git add . git commit -m “message” git push -u origin master these commands to push to my Git Repository.

Then I again wanted to automate the above manual process on a weekly basis, so I created a file inside the same dotfiles directory with the name

cronscript.sh

#
#!/bin/bash
cd ~/dotfiles
bash syncfile.sh
git add .
git commit -m “weekly changes”
git push -u origin master -f 
Enter fullscreen mode Exit fullscreen mode

Finally, I created a weekly Cron job with crontab -e

0 0 * * 0 bash /home/ujwal/dotfiles/cronscript.sh

Conclusion

Every week my config files are getting synced automatically and I don't need to remember at all.

There are other great tools for managing dotfiles that have many more features than the above script like https://github.com/jbernard/dotfiles.
I simply made my own script as I could learn bash scripting side by side too.

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

Why do you use sudo for every copy command? The implication is that your user account doesn't have access to things in its own home directory. You're then running a chown command to make sure everything's back to normal at the end. What's the reasoning there?

I recommend using something like GNU stow to handle this stuff automatically.

Collapse
 
ujwaldhakal profile image
ujwal dhakal

Yep, thanks for pointing it out. Actually, dotfiles was created by other user so i had to use sudo and unknowingly i put in all codes since these code works without sudo too as i have chown my home with the current user ujwal