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 search to search for files to find
4 find-print0 | xargs -0 … : use zeros to separate elements, not spaces, to accept 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
2 the recursive search begins in the current directory
3 it’s 2x faster than find + xargs + grep as shown here.

To list the ±100 supported file types:

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

      .ada  .adb  .ads

      .asm  .s

      .bat  .cmd
$ ag --list-file-types | wc -l

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