Skip to content

Instantly share code, notes, and snippets.

@rpavlik
Created February 24, 2012 21:35
Show Gist options
  • Save rpavlik/1903905 to your computer and use it in GitHub Desktop.
Save rpavlik/1903905 to your computer and use it in GitHub Desktop.
Move new PDF files out of your downloads folder and into your reference manager's watched folder.
#!/bin/bash
# Author: Ryan Pavlik <[email protected]> <http://academic.cleardefinition.com>
# Requires inotify-tools to be installed. Make sure SRCDIR ends in a trailing slash.
SRCDIR=${HOME}/Downloads/
DESTDIR="${HOME}/Dropbox/Bibliography/Mendeley Watched Folder/"
function watchForPdfs()
{
inotifywait -e create --format %f --exclude '.crdownload$' -m ${SRCDIR}
}
function computeDestinationName()
{
srcname="${1}"
destname="${DESTDIR}/${srcname}"
while [ -f "${destname}" ]; do
destname="${DESTDIR}/${RANDOM}${srcname}"
done
echo "${destname}"
}
function movePdf()
{
srcname="${SRCDIR}${1}"
destname=$(computeDestinationName "$1")
echo "$1 -> ${destname}"
mv "${srcname}" "${destname}"
}
function handleLines()
{
while true; do
read fn
# Perhaps replace with a smarter ignore regex?
if echo "${fn}" | grep "[.]pdf$" > /dev/null; then
movePdf "${fn}"
else
echo "${fn}: skipping"
fi
done
}
watchForPdfs | handleLines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment