BurntSushi / ripgrep

ripgrep recursively searches directories for a regex pattern while respecting your gitignore
The Unlicense
46.28k stars 1.93k forks source link

Searching filenames (`-g` option in `ag`) #91

Closed Valloric closed 7 years ago

Valloric commented 7 years ago

Ag supports using -g to recursively search filenames matching the provided pattern. Ripgrep doesn't have this feature (to the best of my knowledge; I've read the --help output three times looking for it); it's the only ag feature holding me back from fully switching to ripgrep. :)

Since ripgrep already has something unrelated mapped to -g, the option name should be something else. The incompatibility with ag will be unfortunate, but survivable.

AtomicNess123 commented 3 years ago

Then, it makes no sense. It works in your example. But I want to find any filename with "helm" in the name. If I search from root or any other folder, it only finds the files at the .emacs.d/modules and .emacs.d/other, but not in .emacs.d/elpa. It is very strange. But when I enter the elpa directory and run the command from there, it finds all the files.

AtomicNess123 commented 3 years ago

Ok... does .gitignore affect results? It seems elpa is in .gitignore!

BurntSushi commented 3 years ago

Of course. The first two sentences of the readme:

ripgrep is a line-oriented search tool that recursively searches your current directory for a regex pattern. By default, ripgrep will respect your .gitignore and automatically skip hidden files/directories and binary files.

This is why I suggested the --debug flag. It will tell you which things ripgrep skips and why.

AtomicNess123 commented 3 years ago

Yes, you were right. I'm just newbie in command line functions. I wonder if it's possible to override .gitignore setting?

BurntSushi commented 3 years ago

Sure. See the guide. If you have a specific question then open a new discussion post.

sergeevabc commented 3 years ago

Sorry to dig up this issue once again, but as a Windows user I believe workarounds provided earlier are not sterling, because alias rgg that combines --files -g part-of-filename is a Linux thing and fd utility to use instead of rg to search for filenames is still buggy to be relied upon. Yet this task arises daily (at least on my end), so I would vote with both hands for a short switch (to remember it easily and not to type long now imaginary --search-for-filenames). For example, -ff.

BurntSushi commented 3 years ago

Other software being buggy is not something I'm receptive towards as a motivation for adding more features to ripgrep.

AtomicNess123 commented 2 years ago

piggybacking off of @anishmittal2020 This function returns any file name AND directory matches, though directories themselves cannot be returned, as the --file flag returns a list of files only, directory paths are excluded.


function f() {
  #find any files or directories that match arg
  rg --files "${PWD}" | rg --regexp ".*/.*$1.*" #| sort | nl
}

Thanks for this. What is the meaning of "${PWD}"?

chris-w-jarvis commented 2 years ago

Whats wrong with doing rg --files | rg foo ?

This seems to work fine for me :)

foucist commented 2 years ago

Inspired by the earlier alias, I'm trying something like this. Function is named ag because old habits die hard 😅

function ag() { rg --files | rg ".*/.*$1.*" | sort | rg $1 }
mcandre commented 2 years ago

Appreciate the snippet! Good to know.

One reason to bake this functionality into rg itself, is that sort may have different syntax on Windows.

And any sorting is most efficiently done with appropriate data structures as early as possible (e.g., "online" algorithm), rather than after the fact.

On Wed, Mar 23, 2022, 3:26 PM James Robey @.***> wrote:

Inspired by the earlier alias, I'm trying something like this. Function is named ag because old habits die hard 😅

function ag() { rg --files | rg "./.$1.*" | sort | rg $1 }

— Reply to this email directly, view it on GitHub https://github.com/BurntSushi/ripgrep/issues/91#issuecomment-1076787036, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAABJRGFSUSKJF77H426E3LVBN5AJANCNFSM4CQYDALA . You are receiving this because you were mentioned.Message ID: @.***>

PyKoch commented 1 month ago

In case somebody wouldn't know the syntax for doing a search with the condition of excluding files with a certain name using the command line, here is an example to do so:

$ rg "from_str" -g "\!tags" -g "repos/*"
                    ^^^

As shown, you may need to escape ! with \, if wish to exclude files from search.

Please excuse the newbie question here: I am trying to search all pdf files and want to exclude those that contain 'ms' in the file name. This is what I entered: rg --type pdf -i "velocity" --sort-files -g '\!ms' -g '*paper_3*' >> test_p3_no_ms.txt Sadly, there are still files with 'ms' in the title included. Any help would be greatly appreciated.

BurntSushi commented 1 month ago

@PyKoch Please don't ask questions in old unrelated issues. This issue is a feature request for some other feature.

And when you do ask questions, please include a reproduction. Here's how you do it: you include all relevant details so that others can see what you're seeing. This means providing inputs, showing the outputs you see and the outputs you want to see. Basically, it comes down to "show, don't tell." Here, I'll show you.

First create an empty directory with some files:

$ mkdir rg-search-issue
$ cd rg-search-issue
$ touch msfoo fooms ms quux

And now run rg --files to show that it correctly finds all of the files I created above:

$ rg --files
quux
ms
fooms
msfoo

And now my attempt at filtering:

$ rg --files -g '\!ms'
$

If you did that, then folks helping you wouldn't have to guess at your problem.

The first thing to realize here is that ripgrep isn't printing any files. This seems different from what you report where there are files being printed. Since you didn't share any details about your environment, and you provided a command more complex than necessary to demonstrate your specific issue with filtering out things that contain ms, I'm not sure what's going on.

In any case, there are two issues here. Assuming you're on Unix, the first issue is that you're escaping ! in a single quoted value. A comment above said that ! needed to be escaped, but that was in a double quoted string. The second issue is that the glob ms doesn't match AAmsAA. The glob ms only matches ms. You need a wildcard to match around it. You seemingly did this for *paper_3*... So just do it for ms:

$ rg --files -g '!ms'
quux
fooms
msfoo
$ rg --files -g '!*ms*'
quux

In the future, please don't ask questions in old issues. There's an entire discussion forum open for that purpose.