Script - Find orphaned links (linking to no existing note)

Hi,

Just like in the case described in How to find links without notes?, I:

  1. often plan future notes from the one I am working on at the moment.
  2. Not to lose my focus, I would like to create these notes later as I do not have the time to take care of them at the moment.
  3. So, what I do to remember to make these notes is that I mark up the links using the double square brackets notation but I do not visit them right away - which does not create them.

–> The main concept with this workflow is that I needed a way to get back to these notes still in the yet-to-be-created stage.

–> I figured it should be possible to recursively search a vault folder for notes (markdown file) and links (identified by their special [[ ]] syntax in the files themselves) and then compare the 2 lists to list out the links with no notes actually created.

–> I fumbled my way using shell scripts but I am quite happy with the result that I can now call from anywhere with Keyboard Maestro (I actually automated calling it, getting the list, making a new note in Obsidian, and pasting it there so that I can process it from within the app itself by just following the links & making the notes on the spot).

Here are the script details, feel free to optimize, I am by no mean a shell expert.

Requirements:

  • Mac (I guess it’d work on Linux and Windows with some minor adaptation)
  • gfind (installed with brew)

Script adaptation for your setup:

  • Replace gfind_path by the path where it installed (can be found by typing which gfind in the terminal)
  • Replace VAULT_PATH by the path to your obsidian vault

How to use?

  • Save the script somewhere on your disk (I put it in my obsidian vault) with the extension .sh
  • Make executable by opening a terminal in this folder and running chmod +x PATH_TO_SCRIPT
  • Run from the terminal by putting the path to the script and pressing enter

The script:

    #!/bin/bash

    # find_orphaned_obsidian_links.sh
    # Written by Marcmagn1, October 2020

    # Step 1 
    gfind_path VAULT_PATH -name '*.md' -type f -printf "%f\n" | sort -u| sed "s/\.md//" | tee VAULT_PATH/notes_list.txt > /dev/null

    # Step 2)
    grep -roiIEh "\[\[([a-zA-Z0-9 ]*)\]\]" VAULT_PATH | sort -u | sed "s/\]]//" | sed 's/\[\[//' | tee VAULT_PATH/links_list.txt > /dev/null

    # Step 3)
    grep -Fxv -f VAULT_PATH/notes_list.txt VAULT_PATH/links_list.txt | sed 's/^/[[/' | sed 's/$/]]/

See the full automation in actin with the following gif (not much to see says how well it works :smile: )

4 Likes

Does anybody have a way to do this on Windows yet? Preferably one that doesn’t require a ton of steps or technical knowledge? Someone in the Discord mentioned a possible extension, but I couldn’t find it.

1 Like

Perhaps its this: https://github.com/graydon/obsidian-dangling-links

5 Likes

Thank you! Yes that looks like what I was looking for. I don’t know why I had such a hard time finding it – now that I’ve gone to install it I see that there’s this whole nifty plugin list I could have just flipped through…

Still getting the hang of things, but loving Obsidian so far!

1 Like