0

I don't know if this is a duplicate questions or not, but I haven't been able to find it...

I'm an old-school UNIX/Linux/AIX programmer, so I'm used to the terminal command line, even on MacOS X.

I've been using the following command to locate the given text in all files under the current folder:

find . -type f 2>/dev/null -exec grep [text] {} \; -ls

where [text] is the text string that I'm searching for. I have two issues, one minor and one major:

Minor - this command displays the text before the path to the file that contains the text string

Major - this command is both CPU (minor) and memory (major) intensive, grabbing almost all available memory while it's running on folders containing large numbers of files.

What I would like to find is a solution that resolves both of these issues, but the resource issue is the more annoying one.

Thanks in advance for any help....

BTW, I checked How do I find all files containing specific text on Linux?, and it doesn't resolve the resource/time issue.

Community
  • 1
  • 1
miwalsh
  • 111
  • 2
  • 9
  • This may be a silly question, and I don't know how grep in MacOS compares to the Linux version, but wouldn't you be better off doing a **grep -r [text] .** ? Of course that's not an option if you're after more than just the path of the file. – tink Jul 11 '14 at 01:28
  • Linux will buffer aggressively, and appear to be using a lot of RAM, when in fact the RAM is still available. How are you determining that the process is using almost all available memory? Second, have you tried "nice" command? – codenheim Jul 11 '14 at 01:28
  • For memory monitoring, I'm using "Memory Clean" from FIPLAB Ltd., which tells me how much physical memory is currently in use. I have not tried using the "nice" command; how would I use that to accomplish the same result, including using "/" as the current directory to scan the entire machine in less than 3 hours? – miwalsh Jul 11 '14 at 01:32
  • @tink I'm also looking for the content of the line containing the text, so "grep -r" doesn't really give me everything I need, but thanks! – miwalsh Jul 11 '14 at 01:39
  • I assume it's printing the matching text before the path because you have the `-exec grep...` before the `-ls`. Try switching them around. Alternatively, get rid of `-ls` and use the `-H` flag with `grep`. – ooga Jul 11 '14 at 01:46
  • @ooga If I do that, it displays the names of all files scanned, whether or not they contain the string that I'm searching for, which just complicates the output.... – miwalsh Jul 11 '14 at 01:48
  • 1
    Try the `-H` flag with `grep`. – ooga Jul 11 '14 at 01:49
  • @ooga Thanks, that solves the minor problem: by using "-H" as an option to grep, it prepends the path to the file for each located line, so I can remove the "-ls" from the find command. – miwalsh Jul 11 '14 at 01:55
  • @miwalsh: grep on MacOS must be quite different from GNU grep, then. When I run **grep -r [text] .** against a directory structure it displays the path of the matching file(s), followed by a colon, and then the actually matching line. – tink Jul 11 '14 at 02:41

0 Answers0