baruch
baruch

Reputation: 157

How to delete unused org-mode attachment files from disc

In org-mode, after deleting a lot of headlines that have had attachment files, the unrefereced files now remain on the disc in my data subdirectory.

Is there a function or script that finds all unrefereced files and does a cleanup?

Upvotes: 3

Views: 743

Answers (1)

kosta
kosta

Reputation: 146

I faced the same problem today after messing with org-capture-templates and then deleting a bunch of entries that did not come out the way I wanted.

I wrote down this script, which gets the job done (for me).

#!/bin/sh

## Location where org-mode stores attachments
datadir="$HOME/Dropbox/Documents/Org/data";
orgdir="$HOME/Dropbox/Documents/Org/"

echo "The following files appear orphaned:";

files=$(find "$datadir" -type f|perl -ne 'print "$1\n" if /([^\/]*)\/[^\/]*$/'|uniq|while read id; do grep -qiR --include "*.org" "$id" "$orgdir" || find "$datadir" -ipath "*$id*" -type f; done)

echo "$files"

if [ "" == "$files" ]; then
   echo "Nothing to do!"
   exit
fi

echo "Delete? y/[n]"
read delete
case $delete in
    y)
        echo "$files" |
        while read fn; do
        rm "$fn";
        done
    echo "Done."
        ;;
    *)
        echo "Not deleting anything!"
        ;;
esac

Upvotes: 3

Related Questions