unix: odd behavior w/ 'find' command

zimv20

macrumors 601
Original poster
Jul 18, 2002
4,390
9
toronto
the other day i tried running a certain construct of the find command, via command line, but it didn't work:

Code:
% find . -type f -exec grep foo {}\; -print
it just sat there, never returned (even on my very small test case). i've used this construct for years, so i know that it works (on some or most unix implementations, anyway).

i was able to find a workaround (for those interested):

Code:
% find . -type f -print | xargs grep foo
(sadly, the workaround doesn't work all the time, as xargs was choking on something coming through the pipe and had to restrict its running to a subdir)

any unix heads know what's up w/ the first find? has 'find' changed since i was doing all my unix coding?

i'm running 10.2.2, if that matters.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,939
4,178
Los Angeles
May I ask what the purpose of the command is? Maybe you don't need the -print option if you use other grep switches, e.g.,

% find . -type f -exec grep -l foo {} \;
 

zimv20

macrumors 601
Original poster
Jul 18, 2002
4,390
9
toronto
Originally posted by Doctor Q
May I ask what the purpose of the command is? Maybe you don't need the -print option if you use other grep switches, e.g.,

% find . -type f -exec grep -l foo {} \;
i believe the print lets me know in which file the grep hit is in.

does this work on your machine?

oh -- the purpose is to search a subdir for files containing the string "foo". well, okay, i was searching for "smtp." but you get the idea.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,939
4,178
Los Angeles
Yes, the find command that I suggested works for me - to display the names of files in the current directory or any subdirectory, recursively, that contains the string "foo". The -l switch of the grep command means "display filename instead of displaying the matching line".

Please note that a space before the \; is required, as in the example I gave.
 

zimv20

macrumors 601
Original poster
Jul 18, 2002
4,390
9
toronto
Originally posted by Doctor Q

Please note that a space before the \; is required, as in the example I gave.
DOH!

that's it. thank you. gee, i actually knew unix once upon a time...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.