Meaningless Drivel is fun!*
The moose likes Linux / UNIX and the fly likes grep the String under subdirectories - --exclude not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "grep the String under subdirectories - --exclude not working" Watch "grep the String under subdirectories - --exclude not working" New topic
Author

grep the String under subdirectories - --exclude not working

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1376
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

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1376
--exclude-dir=DIR is not working too.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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

Joined: Mar 28, 2003
Posts: 11465
    
  94

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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: grep the String under subdirectories - --exclude not working