Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!lll-winken!sun-barr!newstop!texsun!vector!egsner!ntpal!herrage From: herrage@ntpal.UUCP (Robert Herrage) Newsgroups: comp.unix.questions Subject: Re: grep Message-ID: <1047@ntpal.UUCP> Date: 25 Oct 90 12:52:32 GMT References: <1990Oct23.123025.18012@kodak.kodak.com> Organization: Bell Northern Research, Inc., Richardson, TX Lines: 43 In article <1990Oct23.123025.18012@kodak.kodak.com>, tiefel@sunshine.Kodak.COM (Lenny Tiefel) writes: > I have a main directory with hundreds of subdirectories, > and I want to find a file with a particular string, say "xyz" > The grep command only works in one directory at a time. Is there > a way of searching my whole directory structure to find a file > with a particular string? Here's a nice implementation, appropriately named "rgrep" (Recursive GREP): #!/bin/sh { find . \( -name '*.*' \) -exec grep -l $* {} \; -exec grep -n $* \; -exec echo \; | more; } By getting into the top-most directory and typing rgrep xyz you would get something like this: ./subdir1/file1 136: this line has xyz in it 210: this line also has xyz in it ./subdir2/subsubdir4/file2 12: this line has xyz in it I believe the "grep -l" causes the "./subdir/file1" to be printed and the "grep -n" causes the line numbers to be printed. The "echo", of course, gives you a blank line separation in case the string exists in more than one file. If you want to limit your searches to specific file extensions, you could replace the "\( -name '*.*' \)" with something like \( -name '*.[chCH]' -o -name '*.ec' -o -name '*.txt' \) which means only files with a ".c", ".h", ".C", ".H", ".ec", or ".txt" extension will be searched. Enjoy! Robert (Thanks Dana Cavasso, author!)