Grep Include Only *.txt File Pattern When Running Recursive Mode

I‘m using Debian Linux as my development workstation. I would like to search a directory called ~/projects/ recursively for “foo” word only for *.txt files. How do I search all text files in ~/projects/ for “foo” word using grep command?

The grep command supports recursive file pattern

Tutorial details
Difficulty level Easy
Root privileges No
Requirements grep
Est. reading time N/A
option as follows:

grep -R "pattern" /path/to/dir/

To limit your search for *.txt, try passing the --include option to grep command

Syntax and examples for --include option

The syntax is:

grep -R --include=GLOB "pattern" /path/to/dir
grep -R --include="*.txt" "pattern" /path/to/dir
grep -R --include="*.txt" "foo" ~/projects/

You can include 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. You can ignore case distinctions in both the PATTERN and the input files with -i optoon i.e. case-insensitive search. In this following example, search for all *.py, *.pl, and *.sh files for “main” word in my /raid6/projects/sysmanagement/ directory:

grep --color -Ri --include="*.py" --include="*.sh" --include="*.pl" "main" /raid6/projects/sysmanagement/

OR

grep --color -Ri  --include=*.{py,pl,sh} "main" /raid6/projects/sysmanagement/

OR a safer option would be (note –color removed and * replaced with \*):

grep -Ri  --include=\*.{py,pl,sh} "main" /raid6/projects/sysmanagement/

The --include option provides you the following advantages:

  1. Speed up the search.
  2. Only match given file pattern.
  3. Do not search for binary files such as compiled files or image files. In other words only look for *.txt or *.py file patterns and so on.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • better Oct 25, 2012 @ 19:32

    this isn’t portable and includes lots of annoying GNUisms.

    It is better to use find . -name \*.txt | xargs grep

  • Ryan Oct 26, 2012 @ 1:08

    It’s really a awful way to use grep that I havn’t seen. I also use find . -name \*.txt | xargs grep before.

  • Chris F.A. Johnson Oct 26, 2012 @ 2:39

    This matches file names; it doesn’t use globbing:

    grep -R –include=GLOB “pattern” /path/to/dir

    In other words, it will include dot files, which globbing does not.

  • Maik K Oct 26, 2012 @ 10:56

    How about enabled globstar(which most ppl I know have anyway) and then grep “foo” /path/**.txt ? Works at least in the richer shells like bash or zsh.

  • Pepe Oct 26, 2012 @ 15:29

    xargs? ewwwwggg. What real men do is:

    find /some/path -type f -name *.txt -exec grep “pattern” {} +

  • Balakrishnan B Oct 26, 2012 @ 16:15

    Zsh Only
    grep “pattern” **/*.txt

    May not work if the number of matching files are too many.

  • Chris F.A. Johnson Oct 26, 2012 @ 21:14

    Balakrishnan, ** also works in bash (version 4) with the globstar option.

  • Sid Burn Nov 16, 2012 @ 10:40

    Real man just use:

    ack –text “pattern”

  • Konrad Dec 28, 2016 @ 11:40

    +1 to find | xargs grep something

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum