Ripgrep: [Question] how to search by filenames only?

4

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

hh9527 picture hh9527  ·  24 Oct 2016

Most helpful comment

24

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
hraban picture hraban  ·  19 Jul 2019

All comments

3

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.
BurntSushi picture BurntSushi  ·  24 Oct 2016
2

@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.*.

YPCrumble picture YPCrumble  ·  21 Dec 2016
24

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
hraban picture hraban  ·  19 Jul 2019
0
--files

how would you do that without the alias?

johnyradio picture johnyradio  ·  26 Aug 2019
4

@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
hraban picture hraban  ·  26 Aug 2019
0

Many thanks! Super helpful.

johnyradio picture johnyradio  ·  26 Aug 2019
2

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.

dzhang-b picture dzhang-b  ·  3 Apr 2020
1

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
}
hraban picture hraban  ·  10 Jun 2020
0

@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
}
Piping picture Piping  ·  12 Jun 2020
0

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)!

anishmittal2020 picture anishmittal2020  ·  29 Jun 2020
0

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 [^]):

anishmittal2020 picture anishmittal2020  ·  29 Jun 2020
0

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.

ssbarnea picture ssbarnea  ·  16 Sep 2020
1

Use fd instead.

BurntSushi picture BurntSushi  ·  16 Sep 2020