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.

When I grep or find something I always get annoyed by the "Permission denied" and "No such file or directory" notice, smth like that:

johndoe@johndoe-desktop:/$ grep -rnP 'YII_CORE_PATH' ./ | grep -v .svn
grep: ./lib/ufw/user6.rules: Permission denied
grep: ./lib/ufw/user.rules: Permission denied
grep: ./lib/init/rw/udev/watch/27: No such file or directory
grep: ./lib/init/rw/udev/watch/26: No such file or directory
grep: ./lib/init/rw/udev/watch/25: No such file or directory

How can I avoid it and make it show only relevant data, i.e. something that I really look for?

share|improve this question

migrated from stackoverflow.com Jan 25 '12 at 18:00

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

4 Answers 4

up vote 16 down vote accepted

with grep you could specifiy the -s flag which does pretty much what @ortang said

-s, --no-messages Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep's -q option. USG-style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead.

with find as far as I know @ortangs answer is the best. something like

find / -name "myfile" -type f -print 2>/dev/null

share|improve this answer
johndoe@johndoe-desktop:/$ sudo grep -rnP 'YII_CORE_PATH' ./ | grep -v .svn

Use the sudo command to elevate the command to have administrative privileges.

share|improve this answer

Redirecting the strerr to /dev/null (a.k.a black hole) is a good way of suppressing permission denied errors.

However, do note that this wound not only suppress permission denied messages but ALL error messages.

If you do wish to retain error messages other than permission denied then you can do something like this -

grep -rnP 'YII_CORE_PATH' ./ 2>&1 | grep -v 'permission denied' > error.log

If you don't wish to retain those then the following would be just fine -

grep -rnP 'YII_CORE_PATH' ./ 2> /dev/null | grep -v .svn
share|improve this answer
1  
You cannot grep something that has been already redirected to /dev/null. –  choroba Jan 24 '12 at 18:20
    
@choroba Have corrected the answer. Meant to write 2>&1 instead of 2> /dev/null for first suggestion. –  jaypal singh Jan 24 '12 at 18:25
    
Yes, you have to redirect stderr to stout first. –  ortang Jan 24 '12 at 18:28

Try redirecting stderr to /dev/null.

johndoe@johndoe-desktop:/$ grep -rnP 'YII_CORE_PATH' ./ 2> /dev/null | grep -v .svn
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.