Skip to content

Instantly share code, notes, and snippets.

@taylorzane
Last active August 29, 2015 14:16
Show Gist options
  • Save taylorzane/a45ee0193c054fc10429 to your computer and use it in GitHub Desktop.
Save taylorzane/a45ee0193c054fc10429 to your computer and use it in GitHub Desktop.
Copy file to Dropbox from shell.
# Throw this at the bottom of your .zshrc.
# Haven't tested this in any other shell, so YMMV.
# This is kinda hacky. First real go at a shell function.
# Feel free to change where your drop config is saved. ~/.drop just worked well.
# Copy file to Dropbox folder.
function drop {
# Usage:
# -d: Temporary dir to send the file to.
# -s: Set the default Dropbox dir to send all files to.
# -l: Echo the default Dropbox dir.
#
# Caveat: -d takes precedence over -s. So `drop -d ~/Dropbox/Dev -s ~/Dropbox-Work/Code my_file.sh` will send the file to ~/Dropbox/Dev, and all subsequent files to ~/Dropbox-Work/Code.
CONFIG=~/.drop # Where the default Dropbox dir is saved to persist shell logouts.
DIR= # One time Dropbox dir
SET_DIR= # Set default Dropbox dir
OUT_DIR= # Output dir to Dropbox (will change depending on flags)
if [ ! -f $CONFIG ]; then
touch $CONFIG
fi
DEFAULT_DROPBOX_DIR=$(< $CONFIG)
COPY_FILE=${@:${#@}} # Get last argument (aka. file to copy.)
while getopts "ld:s:" OPTION
do
case $OPTION in
d)
DIR=$OPTARG
;;
s)
SET_DIR=$OPTARG
;;
l)
if [ $DEFAULT_DROPBOX_DIR ]; then
echo $DEFAULT_DROPBOX_DIR
else
echo "No directory set. Set it with 'drop -s path/to/dropbox'."
fi
return
;;
?)
return
;;
esac
done
if [ $SET_DIR ]; then
DEFAULT_DROPBOX_DIR=$SET_DIR
echo "${DEFAULT_DROPBOX_DIR}" > ~/.drop
fi
if [ $DIR ]; then
OUT_DIR=$DIR
else
OUT_DIR=$DEFAULT_DROPBOX_DIR
fi
if [[ ! $COPY_FILE = $0 ]]; then # If they just ran `drop` then ignore their mistake.
if [[ ! $COPY_FILE = $OUT_DIR ]] && [[ ! $COPY_FILE = $SET_DIR ]]; then # AND If they actually provided a file, actually copy it.
cp -n $COPY_FILE $OUT_DIR
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment