Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a bit of a weird issue.

If I run:

find . -iname '*.php' -o -iname '*.pl' -o -iname '*.html' | xargs grep -i users

I get a load of errors for filenames containing spaces, but also some files match.

However, if I try the following so filenames with spaces in are correctly handled, no matches are returned:

find . -iname '*.php' -o -iname '*.pl' -o -iname '*.html' -print0 | xargs -0 grep -i users

What am I doing wrong? The second command should return me at least the same matching files as the first, but I don't get any matches.

share|improve this question

migrated from stackoverflow.com Nov 10 '12 at 0:05

This question came from our site for professional and enthusiast programmers.

    
Why don't you just add -exec grep -i users {} \; instead of using xargs ? –  dwalter Nov 9 '12 at 15:26
    
It still doesn't work. It works without the additional -o -iname '.pl' -o -iname '.html' clause though –  user1491250 Nov 9 '12 at 15:35

2 Answers 2

And the answer is:

find . \( -iname '*.php' -o -iname '*.pl' -o -iname '*.html' \) -print0 | xargs -r0 grep -i users

This works. I hadn't seen it before but a colleague told me about it.

share|improve this answer
    
Or -iname '*.php' -print0 -o -iname '*.pl' -print0 ...... –  Jonathan Leffler Nov 9 '12 at 15:44
    
You could also use \( -iname '*.php' ... \) -exec grep -i users +, where the plus means 'build up a command line like xargs without needing to use xargs'. Using a semi-colon \; would mean execute grep for each file as it is found. –  Jonathan Leffler Nov 9 '12 at 15:46

You don't need print0 and xargs, this functionality is already built in find:

find . -iname "*.php" -o -iname "*.pl" -o -iname "*.html" -exec grep -i users {} + 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.