Martim Passos
Martim Passos

Reputation: 317

Find files with a specific folder name anywhere in their path

I'm using

find ./*/*/*/FOO/*/*/!(!(*.tif)|*v.tif) -type f

to get all tiff files that do NOT end with v.tif in a directory tree. How do I edit the command to find files with FOO anywhere in their path? I came across globstar but it doesn't seem to be available on mac's default bash.

Bonus question: what would be the Windows prompt/Powershell equivalent for this?

Upvotes: 1

Views: 1439

Answers (3)

Paul Hodges
Paul Hodges

Reputation: 15418

Looks like you're already using shopt's extglob. Add globstar.

shopt -s globstar

Then

find **/FOO/**/ -type f ! -name '*v.tif'

For just TIF's

find **/FOO/**/* -type f -name '*.tif' ! -name '*v.tif'

Upvotes: 2

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30665

for powershell

Get-ChildItem -Path <path> -Recurse -Filter *FOO*.tif -Exclude *v.tif

Use this for including FOO in directory names as well

Get-ChildItem -Path c:\ASB -Recurse -Filter *.tif -Exclude *v.tif -Include *FOO*

more general solution

 Get-ChildItem -Path c:\ASB -Recurse -Filter *.tif -Exclude *v.tif | Where-Object FullName -like *FOO*

Upvotes: 1

thanasisp
thanasisp

Reputation: 5975

To have FOO anywhere in their full path name:

find . -type f -wholename "*FOO*"

additionally to end with .tif and exclude *v.tif:

find . -type f -wholename "*FOO*[^v].tif"

but the above one excludes the edge case of *FOO.tif, so this is better:

find . -type f -wholename "*FOO*.tif" ! -name "*v.tif"

Upvotes: 4

Related Questions