Search code examples
bashpowershellglob

Powershell & bash: get all files matching a directory in the path and file name


I'm looking to operate on all the files in a directory I have that are within some directory (e.g. .../bin/.../myfile.txt), where each ... can be any number of directories deep. In nu shell, I can do this directly using ** (the recursive wildcard) by e.g. doing:

mycommand **/bin/**/myfile.txt

I tried using Get-ChildItem as follows:

Get-ChildItem -Path . -Recurse -Filter myfile.txt

But this gets all instances of myfile.txt, not just the ones nested under a bin/ directory. It also seems like ** is not a thing in powershell, so that's not an option. Is there any straightforward way to do get what I want?

Similarly, what's the way to do the same thing in bash (again, it seems like ** is not available to me)?


Solution

  • It's a little clunky, but one can do the following in powershell:

    Get-ChildItem -Path . -Recurse -Directory -Filter bin | Get-ChildItem -Recurse -File -Filter myfile.txt
    

    The first command finds all the bin/ directories, and then the second one looks within those directories and looks recursively for the file you want. You can chain this together for any number of directories in between.