• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

grep the String under subdirectories - --exclude not working

 
kri shan
Ranch Hand
Posts: 1478
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following grep command gives list of files has String 'test'.
grep -r -l -c "test" *.* ./ > output.txt

Following command for excluding particular directory is not working.
grep -r -l -c --exclude="\.perforce_path" "test" *.* ./ > output.txt --> not working

How do add word count with following grep command? wl -c
grep -r -l -c --exclude="\.perforce_path" "test" *.* ./ > output.txt
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the man page for 'grep' -

--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.


Also, the "*.*" is probably not doing what you think it is. Since the command is being pre-processed by your shell, the "*.*" will be interpreted by the shell (not by grep) as being all the files and directories that have at least one '.' in them and will apply to the current directory only; not recursively. It it the "-r" that makes it recursive and applies to all the directories on the tail of your command line except those specifically excluded by a --exclude-dir argument.
 
kri shan
Ranch Hand
Posts: 1478
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--exclude-dir=DIR is not working too.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:--exclude-dir=DIR is not working too.


Works for me! GNU grep 2.6.3 on Ubuntu 11.04 . Post your corrected command.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11945
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:How do add word count with following grep command? wl -c
grep -r -l -c --exclude="\.perforce_path" "test" *.* ./ > output.txt

Your grep commands are mutually exclusive here - the grep man page tells us that:

-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match.

Therefore there is no point in having both the -l and the -c options to grep, as the list files option will stop the count from succeeding.

I assume you are trying to get a count of the number of files that contain the word test. The command you want is wc (for word count), not wl, and the wc -l will count lines, which I think makes sense.

Presumably you want to continue having the output go to output.txt while simultaneously getting a line count? If so, then you probably want to use the tee command. Now you can have a command like:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic