Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] how to search by filenames only? #193

Closed
hh9527 opened this issue Oct 24, 2016 · 24 comments
Closed

[Question] how to search by filenames only? #193

hh9527 opened this issue Oct 24, 2016 · 24 comments

Comments

@hh9527
Copy link

hh9527 commented Oct 24, 2016

--files will list all files which respect ignore files. Is there a flag to list filenames which match the given filename pattern?

@BurntSushi
Copy link
Owner

From rg --help:

    -g, --glob GLOB ...        Include or exclude files for searching that
                               match the given glob. This always overrides any
                               other ignore logic. Multiple glob flags may be
                               used. Globbing rules match .gitignore globs.
                               Precede a glob with a '!' to exclude it.

@YPCrumble
Copy link
Contributor

@BurntSushi this is related to the issue I just raised, #284. The I believe the question here is "How can I search for a pattern in the filename, and return the filenames that include that pattern in the path/filename?"

It appears that the -g flag limits my search for text within the file to the filenames matching the -g pattern. Does that explain the nuance?

Example:

If there is a file relative to my directory, ./stories/story.txt that contains the text "Once upon a time", I want to be able to run rg -g story and return ./stories/story.txt. It appears that the -g flag would return nothing in this query, but it would return "Once upon a time" if I try rg Once -g story.*.

@hraban
Copy link

hraban commented Jul 19, 2019

I have the following alias to shim this in my ~/.bashrc:

alias rgf='rg --files | rg'

This allows you to do:

$ rgf 2018
lib/lib.es2018.full.d.ts
lib/lib.es2018.promise.d.ts
lib/lib.es2018.intl.d.ts
lib/lib.es2018.regexp.d.ts
lib/lib.es2018.asynciterable.d.ts
lib/lib.es2018.d.ts

@johnyradio
Copy link

--files

how would you do that without the alias?

@hraban
Copy link

hraban commented Aug 26, 2019

@johnyradio just type the command directly:

rg --files | rg 2018

A Bash alias just substitutes the command for whatever the contents of the alias is, then continues evaluating as usual. That's all an alias is:

alias rgf="rg --files | rg"
rgf 2018

=

rg --files | rg 2018

@johnyradio
Copy link

johnyradio commented Aug 26, 2019 via email

matt-tingen pushed a commit to matt-tingen/configs that referenced this issue Mar 7, 2020
@dzhang-b
Copy link

dzhang-b commented Apr 3, 2020

rg --files | rg <pattern> <directory> or rgf <pattern> <directory>won't work. It will search all contents in the directory, not the filename in that directory.

It is best if we can add an actual flag to do this.

@hraban
Copy link

hraban commented Jun 10, 2020

rg --files | rg <pattern> <directory> or rgf <pattern> <directory>won't work. It will search all contents in the directory, not the filename in that directory.

It is best if we can add an actual flag to do this.

$ rg --files [directory] | rg <pattern>

if you want to do this as a "rgf" style alias, use a function instead:

function rgf {
    rg --files $2 | rg $1
}

@Piping
Copy link

Piping commented Jun 12, 2020

@hraban Thanks for the tip, I updated the logic a bit for future newcomer.

rf() {
  if [ -z "$2" ]
  then
      rg --files | rg "$1"
  else
      rg --files "$2" | rg "$1"
  fi
}

@lamyergeier
Copy link

rg --files | rg <pattern> <directory> or rgf <pattern> <directory>won't work. It will search all contents in the directory, not the filename in that directory.
It is best if we can add an actual flag to do this.

$ rg --files [directory] | rg <pattern>

if you want to do this as a "rgf" style alias, use a function instead:

function rgf {
    rg --files $2 | rg $1
}

What you suggested searches in the entire path and not just in the filename (what the OP had asked)!

@lamyergeier
Copy link

lamyergeier commented Jun 29, 2020

To search Python in filenames:

Search=Python
rg --files "${PWD}" | rg --regexp "${Search}[^/]*$" | sort | nl

Note: Use above in Linux and Mac (for windows replace [^/] with [^]):

@ssbarnea
Copy link
Contributor

I still hope to see native support for it. There is a huge UX benefit on having it as a default feature and the number of user upthumbs explains it.

@BurntSushi
Copy link
Owner

Use fd instead.

@AtomicNess123
Copy link

AtomicNess123 commented Feb 7, 2021

rg --files | rg 2018

Thanks, very useful. What does the | mean? Also, this finds filenames that do not match the query because the files contents contain the query. I am only interested in the filename.

@BurntSushi
Copy link
Owner

@AtomicNess123 rg --files | rg 2018 will only print file names that contain 2018.

The | is a shell pipeline. It redirects the output of rg --files into the input of rg 2018.

@AtomicNess123
Copy link

Mmm... I have to digest that. Could you give a more illustrative example? No rush, though...

@BurntSushi
Copy link
Owner

BurntSushi commented Feb 7, 2021

$ touch foo2018 foo bar2018 bar
$ rg --files
foo
foo2018
bar2018
bar
$ rg --files | rg 2018
foo2018
bar2018

At no point are the contents of any files searched in the above example.

@AtomicNess123
Copy link

Thanks. What is the difference then between rg --files | rg 2018 and rg --files -g '*2018*' ?
Thanks for your time!

@BurntSushi
Copy link
Owner

The former will print all paths in the current tree that contain 2018. The latter will only print paths containing 2018 where all of its parent directories also contain 2018. The -g flag influences which directories ripgrep descends into.

@AtomicNess123
Copy link

Thanks, very insightful!

@AtomicNess123
Copy link

Actually, it won't work like that in my case:

rg --files -g '*helm*'                                                                                                       
xxx-helm-xxx.el
helm/xxx-helm-xxx.el
helm/sub2/xxx-helm-xxx.el
helm/sub2/xxxhelmxxx.el
helm/xxxhelmxxx.el
a/xxx-helm-xxx.el
a/sub2/xxx-helm-xxx.el
a/sub2/xxxhelmxxx.el
a/xxxhelmxxx.el
xxxhelmxxx.el

rg --files | rg helm               
xxx-helm-xxx.el
helm/xxx-helm-xxx.el
helm/sub2/xxx-helm-xxx.el
helm/sub2/xxxhelmxxx.el
helm/xxxhelmxxx.el
a/xxx-helm-xxx.el
a/sub2/xxx-helm-xxx.el
a/sub2/xxxhelmxxx.el
a/xxxhelmxxx.el
xxxhelmxxx.el

As you can see, the -g flag makes no difference.

@theicfire
Copy link

Use fd instead.

Probably referencing https://github.com/sharkdp/fd. TIL.

@JosephTLyons
Copy link

Any way to search for all file nams with a given pattern without having to specify a search pattern?

〉rg -g *.json                                                                                                             
ripgrep requires at least one pattern to execute a search

@BurntSushi
Copy link
Owner

Yes. Use the --files flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests