Create tag collections for your Markdown library

Maybe you are like me and maintain all your notes in a folder with thousands of Markdown files in it.

You might not want to always search afresh for all those documents that have the tag "product" you have created.

Let's say your root folder is called "Notes", then you create a .script folder within in (that makes its location Notes/.script).

Create the following script as collect-tags.sh and make it executable with chmod u+x collect-tags.sh. You can now create tag collections with the command ./collect-tags.sh TAG and it will be saved in your inbox (which is hard-coded for now, feel free to change the folder location).

#!/bin/bash

# Check if a TAG argument was provided
if [ -z "$1" ]; then
  echo "Error: TAG argument is required."
  exit 1
fi

TAG=$1

# Get current timestamp in the desired format
TIMESTAMP=$(date +'%y%m%d%H%M')

# Run the ag and awk commands, substituting the TAG and TIMESTAMP variables
cd ..
ag "#$TAG" --markdown | awk -F'|' -v ts=$TIMESTAMP -v tag=$TAG '{sub(":.*", "", $NF); sub(".md", "", $NF); printf "- [[%s]]\n", $NF}' > "_Inbox/${TIMESTAMP}-collection-${TAG}.md"
Did this help you? 👍 👎