July 20, 2023

Search multiple files for an expression (e.g. a keyword) with grep

You can search the content of multiple files for a pattern, that is a keyword for example, and show the list of files which match that pattern to the shell with the utility grep.

The syntax of the command is:

$ grep pattern file.s

For example:

$ grep hello file.txt

will print the lines of the file file.txt where there is an occurence of hello.

$ grep hello *

will print the lines and names of the files in the current directory which content matches hello.

You can also pass option to grep. For example:

$ grep -i hello *

will print the lines and names of files which content matches hello or HELLO.

$ grep -r hello *

will search directories recursively in addition to files in the current directory and print the lines and names of files which content matches hello.

$ grep -d skip hello *

or

$ grep -d s hello *

will print the lines and names of files in the current directory which content matches hello and skip directories.

$ grep -i hello *.md

will print the lines and names of files ending with extension .md in the current directory which content matches hello or HELLO.

$ grep '^Tags:.*grep$' *

will print the lines and name of files which content matches a sentence which both starts with Tags:’ and end with the term grep’.

To learn more about grep and regular expressions see the many more options run $ man grep.

grep is copyleft-licensed and is maintained by Jim Meyering1. It was first released in 1973.

See also:


  1. GNU Grep official website.↩︎


text processing office applications gnu linux trisquel command-line interface (cli) grep wiki shell literacy

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88