Linux – grep Command

The grep command stands for “global regular expression print.” It is used to search for specific patterns within files or output. The patterns can be simple strings or complex regular expressions. grep is a powerful tool for finding and filtering text.



Tutorials dojo strip

Initial Example

Using grep to search for the string “example” in a file called file.txt:

grep "example" file.txt




GREP Parameters

ParameterDescription
-i, --ignore-casePerform a case-insensitive search, ignoring letter case differences
-v, --invert-matchInvert the search results to display lines that do not match the pattern
-r, --recursiveRecursively search through directories and their subdirectories
-w, --word-regexpMatch only whole words that exactly match the pattern
-x, --line-regexpMatch only lines that exactly match the entire pattern
-c, --countCount the number of lines that match the pattern
-n, --line-numberDisplay the line numbers along with the matching lines
-l, --files-with-matchesList the files that contain at least one match
-L, --files-without-matchList the files that do not contain any matches
-H, --with-filenameDisplay the filename for each match
-h, --no-filenameSuppress the filename in the output when searching multiple files
-o, --only-matchingShow only the matching part of the lines, not the entire line
-q, --quietSuppress all normal output and exit immediately after finding the first match
-s, --no-messagesSuppress error messages about nonexistent or unreadable files
--color[=WHEN]Highlight matching strings with color. WHEN can be always, never, or auto
-E, --extended-regexpInterpret patterns as extended regular expressions
-F, --fixed-stringsInterpret patterns as fixed strings, not regular expressions
-P, --perl-regexpInterpret patterns as Perl-compatible regular expressions
--binary-files=TYPESpecify how to handle binary files. TYPE can be binary, text, or without-match
--include=GLOBSearch only files that match the given glob pattern
--exclude=GLOBSkip files and directories matching the given glob pattern
--versionDisplay version information and exit
--helpShow help information and exit




Examples

1. Search for a Pattern in a File

To search for a specific string in a file:

grep "pattern" filename.txt

2. Search for a Pattern in Multiple Files

To search for a specific string in multiple files:

grep "pattern" file1.txt file2.txt

3. Ignore Case Sensitivity

To perform a case-insensitive search, use the -i option:

grep -i "pattern" filename.txt

4. Display Line Numbers

To display line numbers along with the matched lines, use the -n option:

grep -n "pattern" filename.txt

5. Recursive Search

To search for a pattern in all files within a directory and its subdirectories, use the -r (recursive) option:

grep -r "pattern" directory_name/

6. Invert Match

To display lines that do not match the pattern, use the -v option:

grep -v "pattern" filename.txt

7. Search for Whole Words

To match only whole words, use the -w option:

grep -w "pattern" filename.txt

8. Use Regular Expressions

To use extended regular expressions, use the -E option:

grep -E "regular_expression" filename.txt

9. Count Matches

To count the number of matching lines, use the -c option:

grep -c "pattern" filename.txt




Linux Playground

Scroll to Top