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} ./
Those warnings are directed to the
stderr
stream, as opposed to the standard out file descriptor. You can silence the stderr output by adding2>/dev/null
to the end of your command.More directly than filtering the warnings you can disable them by adding
-s
:There are some compatibility issues with this option. However, this shouldn't be a problem for personal use.
I used to get a ton of annoying messages like this:
The reason is that the
--directories
flag is defaulted toread
. I changed it torecurse
; if you don't want it to automatically do a recursive search you can useskip
instead.The easiest way to handle this all the time is to set it in an environment variable. In
~/.bash_profile
or~/.bashrc
depending on your distro:Now it automatically suppresses those messages any time I use grep.
Another option is the
--no-messages
flag, shorthand-s
. This will also get rid of theIs a directory
messages, but it also suppresses other messages which might be more useful. For example, if you're doing a nested search in*/*/*
and no such file of that pattern exists, it won't tell you that.Alternative approach instead of doing
grep
recursively with-ir
would be to letfind
command (which is recursive by default) handle the permissions with-readable
flag and path's to exclude with-not -path "*.svn*"
flags, and then pass the file togrep
. Excluding directories is done via-type f
for finding only regular files.When doing recursive searches in specific files, you are much better off using
ack-grep
. The syntax here would be:To remove the permission error messages, you may want to run the same command with
sudo
:But eventually, if you want to search installed packages, look at those various options: https://www.google.com/search?q=ubuntu%20search%20inside%20installed%20packages