Grimoire-
Command
.es

GNU+Linux command memo

Find text in selected files

Trouver du texte dans une sélection de fichiers

1. grep : present everywhere

$ find "$3" -name "$2" -print0 | xargs -0 grep -i -n --color $1
1 text to search
2 files to find to search in
3 where to search for files to find

The couple of arguments find-print0 | xargs -0 … : produce the use zeros to separate elements, not spaces, thus accepting spaces in file names

2. ag : a faster and simpler command

Or you can use the silver searcher (from the silversearcher-ag Debian package):

$ ag --py $1
1 text to search

The recursive search begins in the current directory.

It’s 2x faster than find + xargs + grep as shown here.

To list the ±100 supported file types:

$ ag --list-file-types
  --actionscript
      .as  .mxml

  --ada
      .ada  .adb  .ads

  --asm
      .asm  .s

  --batch
      .bat  .cmd
 […]
$ ag --list-file-types | wc -l
301

3. rg : an even faster command, written in Rust

Comes with the ripgrep Debian package. Generally 2x faster than ag, and 6x faster than find + xargs + grep as tested here.

$ rg -t py

There are ±130 supported file types:

$ rg --type-list
agda: *.agda, *.lagda
aidl: *.aidl
amake: *.bp, *.mk
asciidoc: *.adoc, *.asc, *.asciidoc
asm: *.S, *.asm, *.s
 […]
$ rg --type-list | wc -l
130