Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use

find -type f -print0 | xargs -0 -n 10 grep -i searchstring

to search recursively for a string. But it also gives me error messages like "permission denied". How can I avoid such error messages and only see results?

share|improve this question

2 Answers 2

Redirect standard error to /dev/null. I.e.

find -type f -print0 2>/dev/null | xargs -0 -n 10 grep -i searchstring 2>/dev/null
share|improve this answer
    
I set an alias for find -type f -print0 | xargs -0 -n 10 grep -i searchstring. Alias is: "mysearch". I search it like this: mysearch searchstring. How will I set your command to an alias? because, in your command searchstring is inside the command, not in the end. –  alwbtc Jun 20 '12 at 9:54
    
A bit dumb but this would do the trick: find -type f -print0 2>/dev/null | xargs -0 -n 10 grep -i searchstring 2>/dev/null | grep -i searchstring –  janos Jun 20 '12 at 13:09

If the error messages come from find (e.g. because there's a directory you can't traverse, redirect its errors to /dev/null.

find -type f -print0 2>/dev/null | …

If the error messages come from grep (e.g. because some files are unreadable), redirect its errors to /dev/null, or pass the -s option to tell it to ignore unreadable files.

xargs -0 grep -s -i searchstring /dev/null

There's no point in limiting the number of files passed together to grep. But you should add /dev/null so that grep always sees at least two files, otherwise grep omits the file name in its output if it has only one file argument. With GNU grep or Busybox (i.e. Linux or Cygwin), you can use the -H option instead.

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.