Recursive grep with only certain types of files


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Recursive grep with only certain types of files
# 1  
Old 01-17-2014
Recursive grep with only certain types of files

Can I please have some ideas on how to do a recursive grep with certain types of files? The file types I want to use are *.c and *.java.

I know this normally works with all files.

Code:
grep -riI 'scanner' /home/bob/ 2>/dev/null

Just not sure how to get it to work *.c and *.java files.
# 2  
Old 01-17-2014
Like this ???

Code:
$ grep --include="*.c" -RIHn "scanner"

# 3  
Old 01-20-2014
Quote:
Originally Posted by Akshay Hegde
Like this ???

Code:
$ grep --include="*.c" -RIHn "scanner"

Do you think there is an error? I tried that and didn't get any output. I let the program run for about an hour until I finally killed it.
# 4  
Old 01-20-2014
Quote:
Originally Posted by cokedude
Do you think there is an error? I tried that and didn't get any output. I let the program run for about an hour until I finally killed it.

Sorry its my fault missed dot (.) Smilie

Try :

Code:
$ grep --include="*.java" -RIHn "pattern" .

Example :

Code:
$ grep --include="*.java" -RIHn "public static void main" .
./9.java:3:    public static void main(String[] args) {
./test1.java:3:    public static void main(String[] args) {
./8.java:3:    public static void main(String[] args) {
./7.java:3:    public static void main(String[] args) {
./10.java:3:    public static void main(String[] args) {
./3.java:3:    public static void main(String[] args) {
./2.java:3:    public static void main(String[] args) {
./5.java:3:    public static void main(String[] args) {
./4.java:3:    public static void main(String[] args) {
./1.java:3:    public static void main(String[] args) {
./6.java:3:    public static void main(String[] args) {
./test.java:3:    public static void main(String[] args) {

# 5  
Old 01-20-2014
Quote:
Originally Posted by Akshay Hegde
Sorry its my fault missed dot (.) Smilie

Try :

Code:
$ grep --include="*.java" -RIHn "pattern" .

Example :

Code:
$ grep --include="*.java" -RIHn "public static void main" .
./9.java:3:    public static void main(String[] args) {
./test1.java:3:    public static void main(String[] args) {
./8.java:3:    public static void main(String[] args) {
./7.java:3:    public static void main(String[] args) {
./10.java:3:    public static void main(String[] args) {
./3.java:3:    public static void main(String[] args) {
./2.java:3:    public static void main(String[] args) {
./5.java:3:    public static void main(String[] args) {
./4.java:3:    public static void main(String[] args) {
./1.java:3:    public static void main(String[] args) {
./6.java:3:    public static void main(String[] args) {
./test.java:3:    public static void main(String[] args) {

Works perfectly Smilie. Thank you. Any idea why you need to include the "."? Usually grep is smart enough to start in your current directory.
# 6  
Old 01-20-2014
Quote:
Originally Posted by cokedude
Works perfectly Smilie. Thank you. Any idea why you need to include the "."? Usually grep is smart enough to start in your current directory.
As far as I know include is just for pattern "." is to start in current directory as you said. similar to find . -type f | xargs .......
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursive Grep with replace

I have seen some useful infomation about recursive grep in one of the thread. Can it is possible to combine resursive grep and replace togather? Means I need to replace old server names in all the files with new server names as we are upgrading our applications. There are lots of files in... (2 Replies)
Discussion started by: yale_work
2 Replies

2. Homework & Coursework Questions

Help with using different types of GREP

1. The problem statement, all variables and given/known data: Hey there, I'm brand new to using Unix as I just started a course on it in my University, and I currently working through a worksheet which focuses on the many commands and methods of GREP (I'm working through the terminal command... (11 Replies)
Discussion started by: SilvarHawke
11 Replies

3. UNIX for Advanced & Expert Users

recursive grep

Hi, on AIX 6.1 , in man document for grep : -r Searches directories recursively. By default, links to directories are followed. But when I use : oracle@XXX:/appli/XXX_SCRIPTS#grep -r subject *.sh It returns nothing. However I have at least one row in a file : ... (3 Replies)
Discussion started by: big123456
3 Replies

4. Shell Programming and Scripting

Help - Bug: A script to compile two types of data files into two temporary files

Dear other forum members, I'm writing a script for my homework, but I'm scratching all over my head and still can't figure out what I did wrong. Please help me. I just started to learn about bash scripting, and I appreciate if anyone of you can point out my errors. I thank you in advance. ... (3 Replies)
Discussion started by: ilove2smoke
3 Replies

5. UNIX for Dummies Questions & Answers

recursive grep output

I'm using this command to get a recursive grep find . -name *.i -exec grep 'blah blah' {} \; -exec ls {} \; now I would like to obtain just the list of the files and not also the line of the file. How should I change the syntax? thank you, (5 Replies)
Discussion started by: f_o_555
5 Replies

6. UNIX for Dummies Questions & Answers

Recursive grep

Hello, First time post - I have no formal unix training and could use some help with this. I have a list of strings in File1 that I want to use to do a recursive search (grep) under a specific directory. Here is an example of the string I need to search: /directory/dire... (16 Replies)
Discussion started by: upstate_boy
16 Replies

7. UNIX for Dummies Questions & Answers

How do I grep in specific file types?

I have a directory with file types ending .log, .mml, .gll, .dll . How can I grep expressions only in say the .log files? (3 Replies)
Discussion started by: bbbngowc
3 Replies

8. UNIX for Dummies Questions & Answers

recursive GREP ?

Hi! Suppose I have a directory (no symbolic links) called /WORK that contains 3 subdirectories: /A /B /C My problem is this: I want to look for a file that contains an order number. So far, I obtain what I want by doing this /home/acb% cd /WORK/A /home/acb/WORK/A% grep '093023553' *.*... (3 Replies)
Discussion started by: alan
3 Replies

9. UNIX for Dummies Questions & Answers

grep recursive directories

I am trying to locate a file or files with specific data in them. Problem is the file(s) could reside in any one of many directories. My question is. Is there a way of recursively greping directories for the file(s) with the data I am looking for. I have tried - 1. $HOME> grep 47518 | ls... (8 Replies)
Discussion started by: jagannatha
8 Replies
Login or Register to Ask a Question