Rename images from 'Screen Shot XXXX' to its MD5 hash

Motivation

I like to have all my notes in the root folder because filing notes in specific folders defeats the purpose of linking notes to many places. (Where should a specific note go when it can fit in different areas?)

Problem

Notes can contain additional attachments. I use screen shots A LOT. The naming convention of the OS is something like ‘Screen Shot 2021-04-22’. This can lead to conflicts with files which have the same name.

Idea

Since the file name does not contain any information anyway, it can be change to something unique. I though about changing it to a unique name.

The plugin could rename the file using its MD5 hash, or something similar. Happy the hear other suggestions.

This renaming can be done when the attachment is added to the note. Additionally, it would be beneficial if a script could run and change the file name and all the links inside the .md documents.

1 Like

This is a good idea!

I had a similar thought/problem when importing notes with Yarle, and wrote a simple Bash script to post process and convert the images to MD5 hash filenames.

Not exactly what you were looking for but this could at least be easily adapted to churn through your vault and rename/dedupe your images.

1 Like

This is a nice starting point. I guess one could use find -name to find all images.
However, any idea how to update the links inside the notes? Just save the current image name in a var and search and replace? If you like, you can create a repo and we’ll work together on it.

@habalver My script does update the links (it uses sed). A backup is made for safety (named whatever.md.bak). I just made an adapted version and put it up on github. Take a look.

1 Like

Hi @luckman212, thank you so much. The renaming of the files works like a charm.
However, I came across a bug: Some links are not updated correctly and they would be essentially “lost” in the attachments folder.

For example:
Screen Shot 2021-04-28 at 8.33.54 PM

But these file had been processed:

Screen Shot 2021-04-28 at 8.39.06 PM

Any idea how to fix this?

Best,
Greg

@habalver Hmm, yeah the script is not smart enough to deal with ![[attachments/xxxxx.png]] Those should really be just ![[xxxxx.png]] in Obsidian’s format. I will think about a way to handle that situation, but for now I suggest you edit that note, remove the “attachments/” prefix in the Wikilink and drag the Untitled 6, 7 etc back into the attachments folder (they should be in the (backups-XXXXX folder) and re-run the script, it should process it successfully after that.

@habalver I just pushed an update to the script, it should handle links with the path prefix now e.g. ![[attachments/xxxxxxxxxxx.png]]. If you want to give it a try.

There are also two interesting plugins available: Unique attachments and Consistent attachments and links, both from the same creator.

Be sure to read the documentation carefully and make backups when starting to play with it, but so far it works perfect for me, especially the combination of both.

The only downside I found so far is regarding embedded pdf’s: when refering to a certain page within the file using the # page = command, the plugin seems to handle it as a header and therefore thinks its a “broken link”, but other than that I only have positive experiences with it!

1 Like

@u1mp3 Good point on those plugins. I’m aware of them as well. One important note about those plugins is that they don’t support wikilinks. From the readme:

The plugin is only compatible with standard markdown links. Wikilinks are not supported.

Ah that’s true, forgot to mention that tiny little detail…:sweat_smile: It’s probably a no-go for many, so thanks for adding that!