InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Grep tutorials

  • Recursive grep exclude dir
  • Recursive grep with specific file pattern
  • grep without regex (fixed string)
 
  • Home
  • > Tutorials
  • > grep

How to do recursive grep with specific file pattern

By admin on Nov 16, 2015

Recursive grep is pretty handy way to searching a pattern in a directory recursively. Sometimes it is useful to restrict search to specific file pattern (e.g. *.php). One can use the following command for this:

$ grep -r --include="*.php" pattern /path/to/dir

Note that –include=”GLOB” skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and […] as wildcards, and \ to quote a wildcard or backslash character literally.

In case you want to include multiple pattern, you can repeat –include as shown below:

$ grep -r --include="*.php" --include="*.html" pattern /path/to/dir

In case you want to exclude specific file patterns (e.g images, etc.) you can use –exclude as shown below:

$ grep -r --exclude="*.gif" --exclude="*.png" pattern /path/to/dir

Suggested posts:

  1. Recursive grep and excluding directory
  2. Rsync – exclude specific file extension
  3. find – exclude directory or file pattern
  4. Linux – find files containing specific text
  5. Iterate over specific file extension in a dir in shell script
  6. How to sort using a specific field on Linux
  7. How to do grep without regex (fixed string)
  8. How to display wordpress page list with specific custom field value
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, grep, Linux/Unix Command Line, Mac, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2022 InfoHeap.

Powered by WordPress