helm/scripts/org-clean

37 lines
667 B
Plaintext
Raw Normal View History

2015-11-20 01:54:30 -08:00
#!/bin/sh
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
}
if [ "$1" == "-n" ]; then
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
[ -z "$DRY_RUN" ] && rm -f "$file"
echo "Deleted '$file'"
fi
done
done