Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to find the installation directory of a particular package. I have a certain keyword using which I am trying to find a particular file.

During grep, I only want to include cpp or h file type. I do not want the grep to show warnings like Permission Denied or Could not find the Directory. I just want it to display matched files, nothing else. Please suggest how can I do this?

At present I am using

grep "My term" -ir --exclude-dir="\.svn" --include=*.{cpp,h} ./
share|improve this question

3 Answers 3

up vote 12 down vote accepted

Those warnings are directed to the stderr, as opposed to the standard out file descriptor. You can silence the stderr output by adding "2>/dev/null" to the end of your command.

share|improve this answer

More directly than filtering the warnings you can disable them by adding -s:

grep "My term" -sir --exclude-dir="\.svn" --include=*.{cpp,h} ./

There are some compatibility issues with this option. However, this shouldn't be a problem for personal use.

-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. (-s is specified by POSIX.)

share|improve this answer
    
I just installed 13.04 and I am getting the errors which I was not getting with 12.04. Could it be that something changed? I thought it was cool that grep would stop warning about files being directories since in most cases I just don't care! –  Alexis Wilke Oct 12 '13 at 20:34
    
Sorry, I am still on 12.10, so I cannot confirm this. –  SebastianG Oct 13 '13 at 16:07

When doing recursive searches in specific files, you are much better off using ack-grep. The syntax here would be: $ ack-grep -i "My term" --cpp --h

To remove the permission error messages, you may want to run the same command with sudo: $ ack-grep -i "My term" --cpp --h

But eventually, if you want to search installed packages, look at those various options: https://www.google.fr/search?q=ubuntu%20search%20inside%20installed%20packages

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.