Get all uploaded files in Roam Research to Obsidian

for some reason, I had a bunch of image files stored without a filename extension. this confused obsidian on my mac, and it didn’t end up syncing those files up. so of course I wrote some scripts to rename them all, to add the extension, and update references to them. after that, they synced.

remember to back up first (or use git)

first, I needed a list of them all (start this in the vault folder) … I looked at the files first to make sure they’re all .png or .jpg files in reality.

cd _attachment/img
ls |grep -v '\.' | xargs file --extension grep -Ev 'png|jpeg'

if that gives any output, you have some additional filetypes I didn’t, and you’ll have to adjust the perl script accordingly.

next I saved the list of filenames:

ls |grep -v '\.' > /tmp/list.txt
cd ../..

then I wrote a perl script to rename the file (using git mv ... because I have the vault tracked in git; if you don’t, take git out of the command in the script)

here’s how to run the script from the top folder of the vault (I called it ext.pl)

ext.pl < /tmp/list.txt

it took 3 minutes to run on my m1 mac, because I have over 7,000 files, and for every every one of the attachments without an extension, it checks every single file in the vault.

here’s the text of the perl script:

#!/usr/bin/perl -w

# takes an image file with no extension, runs it through file, then assumes it will be jpeg or png, updates all references
my $path = './_attachment/img/';
while ( $line = <> )
{
    chomp $line;
    my $fn = $line;
    chomp $fn;
    my $file = `file --extension $path$fn`;
    chomp $file;
    print "$file\n";
    $file =~ m/$fn: (png|jpeg)/;
    my $ext = $1;

    my $cmd = "find . -name \\*.md -print0 | xargs -0 perl -p -i -e \"s/$fn/$fn.$ext/g\"";
    print $cmd, "\n";
    system($cmd) == 0 or die "$?";
    my $mv = "git mv \"$path$fn\" \"$path$fn.$ext\"";
    print $mv, "\n";
    system($mv) == 0 or die "$?";
}