Site icon LinuxCommands.site

grep command tutorial in linux/unix with examples and use cases

linux grep command – print lines that match patterns

Linux grep searches for PATTERNS in each FILE. PATTERNS is one or patterns separated by newline characters, and grep prints each line that matches a pattern.

A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

Syntax

       grep [OPTION...] PATTERNS [FILE...]
       grep [OPTION...] -e PATTERNS ... [FILE...]
       grep [OPTION...] -f PATTERN_FILE ... [FILE...]

Options

Matcher Selection

Matching Control

General Output Control

Context Line Control

File and Directory Selection

Examples

ylspiritdeMacBook-Pro:grep ylspirit$ ls | grep "test"
ylspiritdeMacBook-Pro:grep ylspirit$ ls | grep -v "test"

ylspiritdeMacBook-Pro:grep ylspirit$ grep -a "13" *
ylspiritdeMacBook-Pro:grep ylspirit$ grep -aR "13" *

More use cases

grep multiple words/patterns/strings, and/or condition, use -e or regex

grep process by name/by id and kill example, and grep -v example

grep count lines/words, count sort

grep not include, grep reverse match

grep only show match

grep multiple patterns

grep show lines before and after

grep or , grep and , grep not condition example

grep regex example

linux ls only files/directories example

Exit mobile version