grep recursively for a specific file type on Linux

前端 未结 4 1378
抹茶落季
抹茶落季 2020-12-23 10:08

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

grep -Rin \"onblur\" *.html

This ret

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 10:25

    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.

提交回复
热议问题