Grep Basics

Grep Basics

Grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search a regular expression and print), which has the same effect: doing a global search with the regular expression and printing all matching lines.

Why use grep?

Grep is a great way to search for text in files. It’s easy to use and can be used to find files that contain a specific word or phrase.

How to use grep

Grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search a regular expression and print), which has the same effect: doing a global search with the regular expression and printing all matching lines.

Grep syntax

Search for a string in a file

grep "string" filename

Search for a string in multiple files

grep "string" file1 file2 file3

Search for a string in all files in a directory

grep "string" *

Search for a string recursively in all files in a directory

grep -r "string" *

Search for a string in all files in a directory and show the file name and line number

grep -rn "string" *

Search for a string in all files in a directory and show only the file name

grep -rl "string" *

Search for a string in all files in a directory and show only the line number

grep -rn "string" *