2015-12-13 03:46:47 -08:00
|
|
|
#!/usr/bin/env zsh
|
2015-11-20 01:54:30 -08:00
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
# A cleanup script for attachments in my org-mode notes.
|
2015-11-20 01:54:30 -08:00
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
ORG_DIR="$HOME/Dropbox/org"
|
|
|
|
ATTACH_DIR=".attach"
|
|
|
|
IFS=$'\n'
|
2015-11-20 01:54:30 -08:00
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
attachment_dirs() {
|
|
|
|
find "$ORG_DIR" -iname "$ATTACH_DIR" -type d
|
|
|
|
}
|
2015-11-20 01:54:30 -08:00
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
attachments_in_dir() {
|
|
|
|
cd "$1/../"
|
|
|
|
find ".attach" -type f
|
|
|
|
}
|
2015-11-20 01:54:30 -08:00
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
is_orphaned_file() {
|
|
|
|
cd "$ORG_DIR"
|
|
|
|
ag -Q "$1" >/dev/null
|
|
|
|
}
|
|
|
|
|
2015-12-13 03:46:47 -08:00
|
|
|
if [[ "$1" == "-n" ]]; then
|
2015-11-27 00:36:47 -08:00
|
|
|
DRY_RUN=1
|
|
|
|
echo "Running dry run!"
|
2015-11-20 01:54:30 -08:00
|
|
|
fi
|
|
|
|
|
2015-11-27 00:36:47 -08:00
|
|
|
for folder in $(attachment_dirs); do
|
|
|
|
cd "$folder"
|
|
|
|
for file in $(attachments_in_dir "$folder"); do
|
|
|
|
if ! is_orphaned_file "$file"; then
|
2015-12-13 03:46:47 -08:00
|
|
|
[[ -z "$DRY_RUN" ]] && rm -f "$file"
|
2015-11-27 00:36:47 -08:00
|
|
|
echo "Deleted '$file'"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|