Skip to content

Instantly share code, notes, and snippets.

@xfbs
Created March 9, 2013 13:33
Show Gist options
  • Save xfbs/5124177 to your computer and use it in GitHub Desktop.
Save xfbs/5124177 to your computer and use it in GitHub Desktop.
A script to keep your dotfiles in sync using Dropbox. Basically, this script backs your local dotfiles up and replaces them with links to the ones in your dropbox.
#!/bin/bash
# dotfiles.sh - a tiny script to keep your dotfiles in
# sync by using dropbox
# this should be the name of the folder where you keep
# your dotfiles.
dotfiles_folder="$HOME/Dropbox/Dotfiles"
# these are the dotfiles to be kept in sync
dotfiles="bashrc concyrc hnbrc screenrc gitconfig vimrc ssh/config irssi/config"
# these are the folders in which dotfiles are kept (e.g. vim, ssh, gnupg, ...)
dotfolders="ssh irssi"
# shows you how your local dotfiles differ to the
# ones in your dropbox
function show_diff() {
for df in $dotfiles; do
if [ -f "$HOME/.$df" ]; then
echo "diff for .$df:"
$dry diff "$dotfiles_folder/$df" "$HOME/.$df"
echo
fi
done
}
# copies the global dotfiles over to your local system
# using a supplied command for installation (cp or ln -s)
function install_files() {
# make dotfolders
for df in $dotfolders; do
if [ ! -d "$HOME/.$df" ]; then
echo "Creating .$df"
$dry mkdir "$HOME/.$df"
fi
done
for df in $dotfiles; do
echo "Installing .$df"
# if there is already a local version of the
# dotfile, back it up (unless it's a link)
if [ -f "$HOME/.$df" ]; then
if [ ! -L "$HOME/.$df" ]; then
$dry mv "$HOME/.$df" "$HOME/.$df.bak"
else
$dry rm "$HOME/.$df"
fi
fi
# install the global dotfile
$dry $1 "$dotfiles_folder/$df" "$HOME/.$df"
done
}
function show_help() {
echo "Usage: $0 <command> [--dry|-d]"
echo "commands: "
echo " diff Shows how the local and global dotfiles differ"
echo " copy Copies the global dotfiles to the local system"
echo " link Links the global dotfiles to the local system"
}
if [ "$2" == "--dry" ] || [ "$2" == "-d" ]; then
dry="echo"
else
dry=""
fi
case $1 in
diff) show_diff;;
copy) install_files "cp";;
link) install_files "ln -s";;
*) show_help;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment