Skip to content

Instantly share code, notes, and snippets.

@kcha
Last active August 14, 2018 19:13
Show Gist options
  • Save kcha/8bf83b3e8a6472b8ffb5a51108fb2b06 to your computer and use it in GitHub Desktop.
Save kcha/8bf83b3e8a6472b8ffb5a51108fb2b06 to your computer and use it in GitHub Desktop.
Backup R Markdown HTML reports
#!/bin/bash
#
# Backup R Markdown authored HTML files to a "notebook" sub-directory and add
# a timestamp to the HTML file
#
# e.g.
# R_analysis.html -> notebook/R_analysis.2015-02-12.html
#
# If file is version controlled, then a short commit hash is also added.
HTML=$1
DIR=notebook
if [ ! -e "$HTML" ]
then
>&2 echo "File doesn't exist!"
exit 1
fi
mkdir -vp $DIR
# Add short git hash if available
HASH=`git rev-parse --short HEAD`
if [ $? == 0 ]
then
NEWHTML="$DIR/`basename $HTML .html`.`date +%F_%H%M -r $HTML`_$HASH.html"
else
NEWHTML="$DIR/`basename $HTML .html`.`date +%F_%H%M -r $HTML`.html"
fi
if [ -e "$NEWHTML" ]
then
echo "Up to date"
else
cp -aiv $HTML $NEWHTML
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment