Skip to content

Instantly share code, notes, and snippets.

@bkrem
Last active April 16, 2019 12:41
Show Gist options
  • Save bkrem/ac1155c8c214ea92e820a5928e65f1b6 to your computer and use it in GitHub Desktop.
Save bkrem/ac1155c8c214ea92e820a5928e65f1b6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# This bash script synchronises dotfiles by exporting each file specified
# in the `dotfiles` array from the `$FROM` directory path to the `$TO`
# directory path.
# If a specified file cannot be found, `cp` will log a path error and
# continue onto the next file.
#
# NOTE
# You can make this script executable by setting the following:
# `chmod +x dotsync.sh`
#
# COMMANDS
# `import <from> <to>`
# `export <to> <from>`
# unicode checkmark char
CHECKMARK="\u2713"
# the dotfiles to be synced
DOTFILES=(.zshrc .bash_profile .gitconfig .vimrc)
# default <source-directory> & <target-directory> parameters
DEFAULT_FROM="$HOME"
DEFAULT_TO=$(pwd) # present working directory
# CLI parameters
DIRECTION="$1"
FROM="${2:-$DEFAULT_FROM}"
TO="${3:-$DEFAULT_TO}"
function sync {
for i in "${DOTFILES[@]}"
do
SOURCE="$1/$i"
TARGET="$2/$i"
cp $SOURCE $TARGET &&
echo "${CHECKMARK} Synced ${SOURCE} -> ${TARGET}"
done
}
case $DIRECTION in
import)
sync $FROM $TO
;;
export)
sync $TO $FROM
;;
*)
echo "arg ${DIRECTION} was not recognised"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment