Cheatsheets
HomeWriteupsResourcesCheatsheets
Linux
Linux
  • Know Yourself and Your System
    • id
    • logname
    • uname
    • w
    • who
    • whoami
  • Surfing / Knowing Your File System
    • cd
    • df
    • du
    • ls
    • pwd
    • stat
    • tree
  • Knowing About Files / Commands
    • alias
    • file
    • info
    • whatis
    • apropos
    • man
    • help
    • history
    • script
  • Manipulating Files / Directories
    • touch
    • mkdir
    • rm
    • rmdir
    • cp
    • mv
    • ln
  • Interacting with Files
    • cat
    • head
    • less
    • middle
    • more
    • tail
  • STROPS / Text Manipulation
    • awk
    • cut
    • grep
    • jq
    • join
    • paste
    • sed
    • sort
    • tr
    • uniq
    • xargs
    • xclip
    • wc
    • tee
    • echo
    • comm
    • diff
    • patch
    • aspell
    • Combos
  • Formatting the Output
    • nl
    • fold
    • fmt
    • pr
    • printf
  • Searching / Finding
    • find
    • locate
    • which
    • whereis
    • type
  • Web Interaction
    • curl
    • wget
  • xxd
  • References
Powered by GitBook
On this page
  • Introduction
  • Important Flags
  • Examples

Was this helpful?

  1. STROPS / Text Manipulation

grep

grep linux command cheatsheet by Thamizhiniyan C S

Introduction

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression.


Important Flags

Flags
Description

-R

Does a recursive grep search for the files inside the folders(if found in the specified path for pattern search; else grep won't traverse directory for searching the pattern you specify)

-h

If you're grepping recursively in a directory, this flag disables the prefixing of filenames in the results.

-c

This flag won't list you the pattern only list an integer value, that how many times the pattern was found in the file/folder.

-i

Specifies grep to search for the PATTERN while IGNORING the case

-l

Will only list the filename instead of pattern found in it.

-n

It will list the lines with their line number in the file containing the pattern.

-v

This flag prints all the lines that are NOT containing the pattern

-E

This flag we already read above... will consider the PATTERN as a regular expression to find the matching strings.

-e

The official documentation says, it can be used to specify multiple patterns and if any string matches with the pattern(s) it will list it.


Examples

Command
Description

Search for the given string in a single file

Checking for the given string in multiple files

Case insensitive search

Match regular expression in files

Checking for full words, not for sub-strings using grep -w

Display N lines after match

Display N lines before match

Display N lines around match

Searching in all files recursively using grep -r

Invert match using grep -v

Display the lines which does not matches all the given pattern

Counting the number of matches using grep -c

Display only the file names which matches the given pattern using grep -l

Show only the matched string

Show the position of match in the line

Show line number while displaying the output using grep -n

Find files with a specific keyword

PreviouscutNextjq

Last updated 12 months ago

Was this helpful?

grep "literal_string" filename
grep "string" FILE_PATTERN
grep -i "string" FILE
grep "REGEX" filename
grep -iw "is" demo_file
grep -A  "string" FILENAME
grep -B  "string" FILENAME
grep -C  "string" FILENAME
grep -r "ramesh" *
grep -v "go" demo_text
grep -v -e "pattern" -e "pattern"
grep -c "pattern" filename
grep -l this demo_*
grep -o "is.*line" demo_file
grep -o -b "pattern" file
grep -n "go" demo_text
grep -iRl [directory path/keyword]