64

Can we search a term (eg. "onblur") recursively in some folders only in specific files (html files)?

grep -Rin "onblur" *.html

This returns nothing. But,

grep -Rin "onblur" .

returns "onblur" search result from all available files, like in text(".txt"), .mako, .jinja etc.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Laxmikant Ratnaparkhi
  • 4,745
  • 5
  • 33
  • 49

5 Answers5

105

Consider checking this answer and that one.

Also this might help you: grep certain file types recursively | commandlinefu.com.

The command is:

grep -r --include="*.[ch]" pattern .

And in your case it is:

grep -r --include="*.html" "onblur" .
Community
  • 1
  • 1
dnl-blkv
  • 2,037
  • 1
  • 17
  • 22
  • 1
    Just so you know, link-only answers are frowned upon on SE. You will want to include a little more information from the original posts :-) – Adrian Frühwirth Mar 06 '14 at 12:48
9
grep -r --include "*.html"  onblur .

Got it from : How do I grep recursively?

Community
  • 1
  • 1
Laxmikant Ratnaparkhi
  • 4,745
  • 5
  • 33
  • 49
4

You might also like ag 'the silver searcher' -

ag --html onblur

it searches by regexp and is recursive in the current directory by default, and has predefined sets of extensions to search - in this case --html maps to .htm, .html, .shtml, .xhtml. Also ignores binary files, prints filenames, line numbers, and colorizes output by default.

Some options -

-Q --literal
          Do not parse PATTERN as a regular expression. Try to match it literally.
-S --smart-case
          Match case-sensitively if there are any uppercase letters in PATTERN, 
          case-insensitively otherwise. Enabled by default.
-t --all-text
          Search all text files. This doesn't include hidden files.
   --hidden
          Search hidden files. This option obeys ignored files.

For the list of supported filetypes run ag --list-file-types.

The only thing it seems to lack is being able to specify a filetype with an extension, in which case you need to fall back on grep with --include.

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
1

Have a look at this answer instead, to a similar question: grep, but only certain file extensions

This worked for me. In your case just type the following:

grep -inr "onblur" --include \*.html ./

consider that

  • grep: command

  • -r: recursively

  • -i: ignore-case

  • -n: each output line is preceded by its relative line number in the file

  • --include \*.html: escape with \ just in case you have a directory with asterisks in the filenames

  • ./: start at current directory.

andreagalle
  • 620
  • 6
  • 17
  • 1
    Anyone who puts an asterisk in a file or directory name deserves all the problems they encounter. – RonJohn Mar 24 '23 at 15:23
1

To be able to grep only from .py files by typing grepy mystring I added the following line to my bashrc:

alias grepy='grep -r --include="*.py"' 

Also note that grep accepts The following:

grep mystring *.html for .html search in current folder

grep mystring */*.html for recursive search (excluding any file in current dir!).

grep mystring .*/*/*.html for recursive search (all files in current dir and all files in subdirs)

Hunaphu
  • 589
  • 10
  • 11