Skip to content

Instantly share code, notes, and snippets.

@sebmaynard
Created June 12, 2013 10:38
Show Gist options
  • Save sebmaynard/5764306 to your computer and use it in GitHub Desktop.
Save sebmaynard/5764306 to your computer and use it in GitHub Desktop.
A bash script to restore dotfiles to ~/ from somewhere else (probably files under a git repository). Allows you to keep *some* of your dotfiles in git without needing everything, and a fast way to restore them on a new box.
#!/bin/bash
function link_files_to_folder() {
if [[ ! -d $1 || ! -d $2 ]] ; then
echo "Usage: link_files_to_folder <source_dir> <target_dir> [prefix]"
exit
fi
SOURCE_DIR=$1
TARGET_DIR=$2
PREFIX=$3
echo "Processing $SOURCE_DIR"
echo
cd $SOURCE_DIR
SOURCE_DIR=$(pwd)
mkdir -p $TARGET_DIR
for i in * ; do
SOURCE_FILE=$i
TARGET_FILE=$PREFIX$i
echo "Processing $SOURCE_FILE"
if [ -e $TARGET_DIR/$TARGET_FILE -o -L $TARGET_DIR/$TARGET_FILE ]
then
echo " Already exists - backing up"
mv $TARGET_DIR/$TARGET_FILE $TARGET_DIR/$TARGET_FILE.`date +"%Y%m%d-%H%M"`
fi
echo " Linking $SOURCE_DIR/$SOURCE_FILE to $TARGET_DIR/$TARGET_FILE"
ln -s $SOURCE_DIR/$SOURCE_FILE $TARGET_DIR/$TARGET_FILE
done
cd ..
echo
echo
}
link_files_to_folder applications ~/.local/share/applications
link_files_to_folder dotfiles ~ .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment