21

I have a couple files, e.g. file-1.org, file-2.org in the same directory sharing the data/ directory for attachments.

I'd like to move file-1.org to a different location, create a new data/ directory in that location and move all attachments that belong to file-1.org to the new directory. Is there an automatic way to do this?

Dan
  • 32,584
  • 6
  • 98
  • 168
abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • 1
    That sounds useful, but doesn't seem to exist. The simplest option may be to move the files directly and then use `org-attach-sync` to sync them with the new item. – glucas Feb 16 '16 at 15:06
  • 1
    Maybe, moving the linked files is not such a good idea. It might be that the files in the data folder are referenced in other org-files where the links break if these files disappear. **Oh no, there are no such org-files** ... _until you become aware of the broken links in the special case you never thought about before_ ;-). Maybe, copying is the better alternative. Or one should at least search for the most obvious cases (e.g., org-files with links in the same folder). – Tobias Feb 18 '16 at 17:11
  • 1
    Are the org files managed by a version control system, like git? When I add attachments to an org file in git, the attachments are automatically committed to the repository. – Melioratus Mar 10 '16 at 23:03
  • org-lint may help find broken links – Alvaro Sep 16 '17 at 11:00

2 Answers2

1

This command could do what you want:

find -E data -type f -iregex ".*($(cat file-1.org | grep -i ':id:' | perl -pe 's/^\s*:id:\s*(\w{2})([\w-]+)$/$1\\\/$2/i' | paste -s -d'|' -)).*" -exec rsync -R '{}' ~/temp/my-new-directory \;

Replace file-1.org with the org file whose attachments you want to move, and ~/temp/my-new-directory with the location of your new directory. A data directory will be created with the attachment files in them (if you want to move instead of copy, pass --remove-source-files to the rsync command).

This was tested on macOS. You might have to omit the -E flag on other platforms.

NOTE: Please do this on a copy of your files. I'm not an expert in emacs, have never used org-mode attachments and I'm not experienced with using this kind of hacky file manipulation. This could blow your org-mode folder into oblivion so back it up first.

0

Based on Marcel Samyn's answer, the following script worked for me under ArchLinux:

find data -regextype awk -type f -iregex ".*($(cat file-1.org | grep -i '^:id:' | perl -pe 's/^\s*:id:\s*(\w{2})([\w-]+)$/$1\/$2/i' | paste -s -d'|' -)).*"  -exec rsync -R '{}' ~/temp/my-new-directory \;

Use the following reduced script to get a list of files to be copied:

find data -regextype awk -type f -iregex ".*($(cat file-1.org | grep -i '^:id:' | perl -pe 's/^\s*:id:\s*(\w{2})([\w-]+)$/$1\/$2/i' | paste -s -d'|' -)).*"

Without the ^ in the grep command, I got false positives from email links containing ":id:".