≡ Menu

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

  • Ryan October 26, 2012, 1:08 am

    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 October 26, 2012, 2:39 am

    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 October 26, 2012, 10:56 am

    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 October 26, 2012, 3:29 pm

    xargs? ewwwwggg. What real men do is:

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

  • Balakrishnan B October 26, 2012, 4:15 pm

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

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

  • Chris F.A. Johnson October 26, 2012, 9:14 pm

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

  • Sid Burn November 16, 2012, 10:40 am

    Real man just use:

    ack –text “pattern”

  • Security: Are you a robot or human?

    Leave a Comment

    You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">