Questions tagged [grep]

grep is a command-line text-search utility originally written for Unix. It uses regular expressions to match text, and is commonly used as a filter in pipelines. Use this tag only if your question relates to programming using grep or grep-based APIs. Questions relating to using or troubleshooting grep command-line options itself are off-topic.

Grep

The name comes from and similar editors, and is derived from global / regular expression / print.

Grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems.

There are many programming languages which include a command or statement called grep; please simply use that language's tag for questions which do not pertain to the Unix utility.

Variations

The following are the several implementations of grep available in some Unix environments:

  • : same as grep -E - Interprets the pattern as an extended regular expression.
  • : same as grep -F - Interprets the pattern as a list of fixed strings, separated by newlines, any of which is to be matched.
  • : displays the processes whose names match a given regular expression.

Common options

  • -f file - Obtain patterns from file, one per line.
  • -i - Ignore case distinctions in both the pattern and the input files.
  • -o - Show only the part of a matching line that matches the pattern.
  • -c - Print a count of matching lines for each input file.
  • -R - Read all files under each directory recursively.
  • -v - Invert the sense of matching to select non-matching lines.

Frequently asked

Other Stack Exchange sites

References

  1. grep/egrep/fgrep man page
  2. pgrep man page
  3. POSIX grep man page
  4. GNU grep manual

Books

  • grep Pocket Reference - "A quick pocket reference for a utility every Unix user needs"
  • GNU GREP and RIPGREP - Step by step guide with hundreds of examples and exercises. Includes detailed discussion on BRE/ERE/PCRE(2)/Rust regular expressions used in these commands.
16456 questions
7328
votes
55 answers

How to find all files containing specific text (string) on Linux?

How do I find all files containing a specific string of text within their file contents? The following doesn't work. It seems to display every single file in the system. find / -type f -exec grep -H 'text-to-find-here' {} \;
Nathan
  • 73,987
  • 14
  • 40
  • 69
4123
votes
13 answers

grep: show lines surrounding each match

How do I grep and show the preceding and following 5 lines surrounding each matched line?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
2118
votes
27 answers

How do I recursively grep all directories and subdirectories?

How do I recursively grep all directories and subdirectories? find . | xargs grep "texthere" *
wpiri
  • 21,337
  • 3
  • 17
  • 6
1881
votes
21 answers

How to grep (search) committed code in the Git history

I have deleted a file or some code in a file sometime in the past. Can I grep in the content (not in the commit messages)? A very poor solution is to grep the log: git log -p | grep However, this doesn't return the commit hash straight…
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
1441
votes
3 answers

How can I use grep to show just filenames on Linux?

How can I use grep to show just file-names (no in-line matches) on Linux? I am usually using something like: find . -iname "*php" -exec grep -H myString {} \; How can I just get the file-names (with paths), but without the matches? Do I have to use…
cwd
  • 53,018
  • 53
  • 161
  • 198
1395
votes
12 answers

How can I grep recursively, but only in files with certain extensions?

I'm working on a script to grep certain directories: { grep -r -i CP_Image ~/path1/; grep -r -i CP_Image ~/path2/; grep -r -i CP_Image ~/path3/; grep -r -i CP_Image ~/path4/; grep -r -i CP_Image ~/path5/; } | mailx -s GREP email@domain.example How…
Jasmine
  • 15,375
  • 10
  • 30
  • 48
1301
votes
3 answers

Negative matching using grep (match lines that do not contain foo)

How do I match all lines not matching a particular pattern using grep? I tried this: grep '[^foo]'
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
1241
votes
11 answers

How can I pipe stderr, and not stdout?

I have a program that writes information to stdout and stderr, and I need to process the stderr with grep, leaving stdout aside. Using a temporary file, one could do it in two steps: command > /dev/null 2> temp.file grep 'something' temp.file But…
user80168
1085
votes
14 answers

How can I exclude directories from grep -R?

I want to traverse all subdirectories, except the node_modules directory.
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
970
votes
15 answers

Can grep show only words that match search pattern?

Is there a way to make grep output "words" from files that match the search expression? If I want to find all the instances of, say, "th" in a number of files, I can do: grep "th" * but the output will be something like (bold is by…
Neil Baldwin
  • 9,739
  • 3
  • 17
  • 5
923
votes
22 answers

Use grep --exclude/--include syntax to not grep through certain files

I'm looking for the string foo= in text files in a directory tree. It's on a common Linux machine, I have bash shell: grep -ircl "foo=" * In the directories are also many binary files which match "foo=". As these results are not relevant and slow…
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
864
votes
13 answers

How to 'grep' a continuous stream?

Is that possible to use grep on a continuous stream? What I mean is sort of a tail -f command, but with grep on the output in order to keep only the lines that interest me. I've tried tail -f | grep pattern but it seems that grep can…
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
822
votes
9 answers

How to grep commits based on a certain string?

In a Git code repository I want to list all commits that contain a certain word. I tried this git log -p | grep --context=4 "word" but it does not necessarily give me back the filename (unless it's less that five lines away from the word I searched…
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
730
votes
18 answers

How do I find files that do not contain a given string pattern?

How do I find out the files in the current directory which do not contain the word foo (using grep)?
Senthil Kumar
  • 9,695
  • 8
  • 36
  • 45
695
votes
14 answers

How can I use grep to find a word inside a folder?

In Windows, I would have done a search for finding a word inside a folder. Similarly, I want to know if a specific word occurs inside a directory containing many sub-directories and files. My searches for grep syntax shows I must specify the…
kiki
  • 13,627
  • 17
  • 49
  • 62
1
2 3
99 100