Skip to content

Instantly share code, notes, and snippets.

@chadbaldwin
Last active February 26, 2025 16:59
Show Gist options
  • Select an option

  • Save chadbaldwin/537a26a59fab2c0630e7ee2f09ae76f3 to your computer and use it in GitHub Desktop.

Select an option

Save chadbaldwin/537a26a59fab2c0630e7ee2f09ae76f3 to your computer and use it in GitHub Desktop.
Testing globs in ripgrep
$string = 'Functions'
$result = foreach ($l in '','*','/','*/','**/') {
foreach ($r in '','*','/','/*','/**') {
$glob = "!${l}${string}${r}"
$stat = Measure-Command { $list = rg --files -g "${glob}" }
[pscustomobject]@{
Glob = $glob
Count = $list.Count
ExecTimeMS = $stat.Milliseconds
}
}
}
$result | ft -AutoSize
@chadbaldwin

chadbaldwin commented Sep 1, 2023

Copy link
Copy Markdown
Author

Returns something that looks like this:

Glob             Count ExecTimeMS
----             ----- ----------
!Functions       24914        284
!Functions*      24476        273
!Functions/      24914        256
!Functions/*     24914        251
!Functions/**    24914        250
!*Functions      24476        278
!*Functions*     24013        250
!*Functions/     24476        252
!*Functions/*    24476        265
!*Functions/**   24476        264
!/Functions      24914        250
!/Functions*     24476        289
!/Functions/     24914        258
!/Functions/*    24914        279
!/Functions/**   24914        254
!*/Functions     25391        258
!*/Functions*    25391        263
!*/Functions/    25391        269
!*/Functions/*   25391        268
!*/Functions/**  25391        271
!**/Functions    24914        272
!**/Functions*   24476        244
!**/Functions/   24914        259
!**/Functions/*  24914        270
!**/Functions/** 24914        277

Keep in mind, this code includes * which means it will also do partial matches of a directory name. For example *Functions will also match with User Functions.

Typically the way I use this script is to first figure out manually how many files should be included in my search by simply selecting and deselecting the directories in Windows Explorer, then viewing properties to get the file count. Then I line that up with the results of this script.

Typically I only need to do this once or twice before I remember how ripgrep handles globs again...It's just not something I use every day, so I tend to forget ๐Ÿ˜‚

In this particular case, I want to exclude the Functions directory but I have some directories that contain the string Functions that I want to include. The file count I'm looking for is 24914. So I'll then look for the simplest glob expression that has that file count, which in this case is simply !Functions

Things get a little more complicated if you want to exclude certain sub-folders, or certain file types under certain sub-folders, etc....For example, "exclude all txt files contained in log directories".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment