Skip to content

Instantly share code, notes, and snippets.

@mskorzhinskiy
Created January 14, 2023 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mskorzhinskiy/e7a21026e7a31216d0f01ab974dbb974 to your computer and use it in GitHub Desktop.
Save mskorzhinskiy/e7a21026e7a31216d0f01ab974dbb974 to your computer and use it in GitHub Desktop.
Various helpers for org-attach feature
#!/bin/env perl
#
# This will try to find orphaned attachments in folder ~/org/storage
# using first argument to this script as a location where org files
# are
#
# Note, I haven't used this function in a while, unsure if it still works
# with most recent org versions...
use strict;
use warnings;
use Data::Dumper;
my $dir = $ARGV[0];
my @data = `find ~/org/storage -maxdepth 2 -mindepth 2 -type d`;
my (@files, @ids);
my @f_ids = ();
foreach my $path (@data) {
my ($p1, $p2) = ($path =~ m/\S+\/(.*)\/(.*)$/);
if (not $p1 =~ m/^\./) {
push @f_ids, "$p1$p2";
}
}
push @files, glob($dir . "/*");
@files = grep { m/.org$/ } @files;
foreach my $file (@files) {
push @ids, map {
m/:ID:\s*(.*)/
} `grep :ID: $file`;
}
sub uniq {
my @arg = @_;
my @uniq = ();
foreach my $val (@arg) {
unless (grep { $val eq $_ } @uniq) {
push @uniq, $val;
}
}
return @uniq;
}
sub array_minus {
my ($a, $b) = @_;
my @result = ();
foreach my $av (@$a) {
unless( grep { $_ eq $av } @$b ) {
push @result, $av;
}
}
return @result;
}
@ids = uniq sort @ids;
my @orphan = array_minus( \@f_ids, \@ids );
foreach my $id (@orphan) {
my $p1 = substr $id, 0, 2;
my $p2 = substr $id, 2;
my $path = "$dir/storage/$p1/$p2";
print "cp '$_' ~/orphan/\n" foreach glob $path . "/*";
print "rm '$_'\n" foreach glob $path . "/*";
print "rmdir " . $path . "\n";
print "rmdir $dir/storage/$p1\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment