如何从光盘中删除未使用的 org-mode 附件文件

How to delete unused org-mode attachment files from disc

在org-mode中,删除了很多有附件的标题后,未引用的文件现在保留在光盘上我的数据子目录中。

是否有函数或脚本可以找到所有未引用的文件并进行清理?

我今天在处理 org-capture-templates 之后遇到了同样的问题,然后删除了一堆没有按照我想要的方式出现的条目。

我写下了这个脚本,它完成了工作(对我来说)。

#!/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 "\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