Skip to content
Advertisement

Using linux find command to identify files that (A) match either of two names (with wildcards) and (B) that also contain a string

The find command is really useful to identify files with a given name that also contain a string somewhere inside of them.

For instance lets say I’m looking for the string "pacf(" in an R markdown file somewhere in my current directory.

find . -name "*.Rmd" -exec grep -ls "pacf(" {} ;

I get useful results.

However, sometimes, I’m not sure if the file I am looking for is an .R file or a .Rmd file so I might also run.

find . -name "*.R" -exec grep -ls "pacf(" {} ;

And lets say there are no R files containing this string so that returns nothing.

One think I’d like to do is look in both .R and .Rmd files for this string. I would think that I could run

find . -name "*.Rmd" -o -name "*.R" -exec grep -ls "pacf(" {} 

But that returns no results.

However if I run

find . -name "*.R" -o -name "*.Rmd" -exec grep -ls "pacf(" {} 

I get the same results as just searching the .Rmd files. So it seems like it is only running the stuff in exec for the second set of files.

Is there a way I could change these commands to look through both the .R and .Rmd files at once?

Advertisement

Answer

Add parentheses ‘()’

find . ( -name '*.R' -o -name '*.Rmd' ) -exec grep -ls "pacf(" {} ;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement