PDA

View Full Version : unix: odd behavior w/ 'find' command




zimv20
Dec 19, 2002, 09:43 AM
the other day i tried running a certain construct of the find command, via command line, but it didn't work:


% 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):


% 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
Dec 19, 2002, 12:43 PM
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
Dec 19, 2002, 11:51 PM
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
Dec 20, 2002, 09:15 PM
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
Dec 21, 2002, 12:04 AM
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...