I am pretty rubbish at the command line despite being someone who likes to take the time to write scripts. grep is a cli tool that I have never got my teeth into as I always tended to default to cmd + shift + f in VSCode. Inspired by this fantastic article on the MDN Blog (which has so much really great context by the way), I decided to use to grep to find an old document from within my legacy Obsidian vaults.
My goal was to find any references to the term “hoc” in an old vault of mine. I need to do the following:
- search the whole directory (grep defaults to a specific file)
- make it case insensitive (I want to match
HOCas well asHocetc) - filter out binary files (which often are a ton of garbled characters under the hood and could turn up matches I am not interested in)
- only target certain file types (namely:
.md,.txtand.mdx)
The last part proved a little difficult as grep only takes a single term per --include arg so I had to delve around in Stackoverflow to find this excellent explanation of how to do this in a more succinct way.
Here is the resultant command:
grep -ri --binary-files=without-match '--include=*.'{md,txt,mdx} "hoc" ./directory-to-targetHere is what the command does:
-rsearches recursively (i.e. on a directory, not just a file)-imakes the search term case insensitive-riis the above two terms combined--binary-files=without-matchmeans thatgrepignores binary files'--include=*.'{md,txt,mdx}is the magic part. Rather than have to write out each file extension I want to include like--include=*.md --include=*.mdxas so on, this command uses some trickery to make the {…} shorthand work. Check out the full explanation here.
It was fun to use grep for this and I hope to get more fluent with it. Despite this whole post, I ended up opening my Obsidian in VSCode and waiting for it to be indexed so I could use the VSCode search. grep was much, much quicker but it is hard to beat the ability to click into the files themselves.