--files
will list all files which respect ignore files. Is there a flag to list filenames which match the given filename pattern?
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 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.*
.
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
--files
how would you do that without the alias?
@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
Many thanks! Super helpful.
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 | rg <pattern> <directory>
orrgf <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 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
}
rg --files | rg <pattern> <directory>
orrgf <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)!
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 [^]):
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.
Use fd
instead.
Most helpful comment
I have the following alias to shim this in my ~/.bashrc:
This allows you to do: