ripgrep no way to exclude directory e.g. Tests and search for all *.c files

-1

What version of ripgrep are you using?

ripgrep 0.7.1
-AVX -SIMD

What operating system are you using ripgrep on?

Mac High Sierra

Describe your question, feature request, or bug.

Imagine a folder structure like:
Feature
codefiles1 (contains *.c, *.h)
codefiles2 (contains *.c, *.h)
tests (contains *.c, *.h)

I would like to search a string str1 in *.c, *.h files however exclude/ignore all occurrences in "tests" folder

I would assume the below syntax would do it?
rg -w str1 -tc -g '!tests/*'

If a feature request, please describe the behavior you want and the motivation.
Please also provide an example of how ripgrep would be used if your feature
request were added.

If a bug, please see below.

If this is a bug, what are the steps to reproduce the behavior?

NA

If this is a bug, what is the actual behavior?

NA

If this is a bug, what is the expected behavior?

ripgrep should have excluded files from "tests" folder

sumonto picture sumonto  ·  28 Feb 2018

Most helpful comment

16

@sumonto you're correct $ rg -w str1 -tc -g '!tests/*' would do what you want. So is that not working for you? It works on my machine with v0.8.1

kevin@beefcake:/tmp/rg$ rg --version
ripgrep 0.8.1
-SIMD -AVX
kevin@beefcake:/tmp/rg$ tree
.
├── code
│   ├── other.h
│   └── some.c
└── test
    └── bar.c

2 directories, 3 files
kevin@beefcake:/tmp/rg$ cat code/other.h 
foo
kevin@beefcake:/tmp/rg$ cat code/some.c 
foo
kevin@beefcake:/tmp/rg$ cat test/bar.c 
foo
kevin@beefcake:/tmp/rg$ rg -w foo -tc -g '!test/*'
code/some.c
1:foo

code/other.h
1:foo
kbknapp picture kbknapp  ·  1 Mar 2018

All comments

1

Please fill out the entire issue template. That's why it is there to guide you, because the request you've made is insufficient to act on. In particular, I have no idea what you are asking for.

Please also make sure you've read the documentation. ripgrep can of course exclude directories.

BurntSushi picture BurntSushi  ·  28 Feb 2018
16

@sumonto you're correct $ rg -w str1 -tc -g '!tests/*' would do what you want. So is that not working for you? It works on my machine with v0.8.1

kevin@beefcake:/tmp/rg$ rg --version
ripgrep 0.8.1
-SIMD -AVX
kevin@beefcake:/tmp/rg$ tree
.
├── code
│   ├── other.h
│   └── some.c
└── test
    └── bar.c

2 directories, 3 files
kevin@beefcake:/tmp/rg$ cat code/other.h 
foo
kevin@beefcake:/tmp/rg$ cat code/some.c 
foo
kevin@beefcake:/tmp/rg$ cat test/bar.c 
foo
kevin@beefcake:/tmp/rg$ rg -w foo -tc -g '!test/*'
code/some.c
1:foo

code/other.h
1:foo
kbknapp picture kbknapp  ·  1 Mar 2018
-5

It's not possible to act on this issue. Closing.

BurntSushi picture BurntSushi  ·  10 Mar 2018
3

I might know what the issue might be: ZSH

I was trying to make this work in ZSH:

rg --stats --ignore-case --type r --max-filesize 200K -g "!packrat" --regexp "stringi"

The command was suppose to only search in specific file types (in this case r type) and search for the text stringi but I want it to exclude everything in "packrat" folders in the following scenario:

.
├── Project1/
│   ├── packrat/
│   │   ├── lib1/R/
│   │   │   └── some_file.R
│   │   └── lib2/R/
│   │       └── some_file.R
│   ├── other_folder/
│   │   └── some_file.Rmd
│   └── some_file.R
│
└── Project2/
     ├── packrat/
     │   └── lib1/R/
     │       └── some_file.R
     └── some_file.R

the ! has a meaning in ZSH, so you should escape the character (except if you use it as an alias). The correct command in my case is:

rg --stats --ignore-case --type r --max-filesize 200K -g "\!packrat" --regexp "stringi"
mmahmoudian picture mmahmoudian  ·  9 Oct 2019
4

You can also just use single-quotes instead of double- (which is what the OP was doing, so i don't think it was the issue there). Or you can turn off history expansion, but you lose some functionality obv

okdana picture okdana  ·  9 Oct 2019