Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

man grep says

grep [OPTIONS] PATTERN [FILE...]

. . .

   --include=GLOB
          Search  only  files whose base name matches GLOB (using wildcard
          matching as described under --exclude).

So I typed

grep --include="*.html" 'li' *

to search for instances of li in HTML files in the current directory. Instead I get a list of every use of li anywhere in the directory, even in the non-HTML files. What am I doing wrong? I tried all kinds of variants like

grep --include=\*.html 'li' *

but without any luck.

share|improve this question

migrated from stackoverflow.com Dec 22 '11 at 4:56

This question came from our site for professional and enthusiast programmers.

    
'grep 'li' *.html' try this –  harish.venkat Dec 22 '11 at 5:20
    
' grep --include="*.html" 'li' * ' works for me please give your grep version using 'grep -version' command –  harish.venkat Dec 22 '11 at 5:44

4 Answers 4

--include=PATTERN only works when you add -r

$ grep -r --include='*.html' 'li' .

-R, -r, --recursive   Read all files under each directory, recursively; this is equivalent to the -d recurse option.
--include=PATTERN     Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN     Recurse in directories skip file matching PATTERN.
share|improve this answer

The shell you're using (bash, tsh, zsh, etc) will dictate the commandline globbing that will and won't work. Most people use bash because it's installed by default on many distros - I use bash.

The following syntax has always worked for me.

    grep -r --include=*.html li *
  1. As mentioned above, the -r switch allows grep to search subdirectories, otherwise it won't, which makes it look like the grep failed.
  2. Simple file type inclusions (one extension) need no tweeking.
  3. Don't quote (single or double) the inclusion, the parser doesn't like it

For inclusions of multiple extensions I use a bash brace expansion.

    grep -r --include=*.{html,htm,js} li *

Other things to try if you are still having trouble

  1. Escape the * to protect it when it's parsed by the shell

    grep -r --include=\*.html li *
    
  2. Make the search path more explicit

    grep -r --include=\*.html li ./*
    

Hope that helps

share|improve this answer

find $directoryname -maxdepth 1 -name '*.html' | xargs grep 'li' Here -maxdepth 1 is for searching only in the directory and not its subdirectories. If you want to search even in sub directory remove -maxdepth 1 in the command.

share|improve this answer
    
The same old error: you do not take into account filenames with spaces, where your command fails. –  enzotib Dec 22 '11 at 6:59

grep -version doesn't work, but grep --version gives

grep (GNU grep) 2.9

and the copyright statement.


grep 'li' *.html

gets the shell to do the globbing, which isn't useful to me since I want to recurse.


I originally tried the command with -R (or -r) but that didn't work either. The command recurses but brings up files without html in the name (or anywhere in the path). So that's not it either.


I'd post these as comments or edit into the original but for some reason StackExchange isn't properly associating my account. (Mods: please comment-ify this!)

share|improve this answer
    
is the find one is also not working??? –  harish.venkat Dec 22 '11 at 6:55
    
@harish.venkat: I can't quite get it to work, but it seems the most promising at the moment. I was working on a similar solution before I posted but couldn't get anywhere; this puts me closer. But I'm still getting strange "No such file or directory" errors even though the output of find alone seems OK. My actual string: find . -name '*.html' | xargs grep -l 'li'. –  Charles Dec 22 '11 at 7:04
    
OK, adding -0 to xargs takes care of the file not found errors, now I just need some kind of 2>/dev/null magic to stop the errors in find. –  Charles Dec 22 '11 at 7:08
    
Also there's an xargs: argument line too long error; I don't imagine there's an easy fix to that? –  Charles Dec 22 '11 at 7:09
    
@Charles You need to create an account on Unix & Linux with the same openid that you used on Stack Overflow and associate your cookie-based account with it. –  Gilles Dec 22 '11 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.