58

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} ./
Kees Cook
  • 17,243
  • 9
  • 68
  • 96
Neeraj Gupta
  • 683
  • 1
  • 5
  • 6

5 Answers5

72

Those warnings are directed to the stderr stream, 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.

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
James Henstridge
  • 40,476
  • 13
  • 109
  • 92
  • 2
    Te be more specific ```find . 2>/dev/null | grep 404.html``` is what a command would look like – csga5000 Mar 21 '18 at 21:12
  • Not the end of the command, but at the start of the command. (arch linux) example : `2>/dev/null ifconfig | grep inet` This will show you your local ip without any error. – Khan Saad Sep 27 '22 at 08:12
34

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.)

  • 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. – Sebastian vom Meer Oct 13 '13 at 16:07
5

I used to get a ton of annoying messages like this:

grep: commands: Is a directory
grep: events: Is a directory
grep: views: Is a directory

The reason is that the --directories flag is defaulted to read. I changed it to recurse; if you don't want it to automatically do a recursive search you can use skip 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:

export GREP_OPTIONS='--directories=recurse'

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 the Is 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.

andrewtweber
  • 150
  • 2
  • 6
  • good explanaition – aldr Oct 16 '19 at 08:31
  • The downside of the `--directories=recurse` option, of course, is that it always performs a recursive grep, even when you know that you just want to grep files in the current folder. Problematic in an era of `node_modules`, for example. – jarmod Mar 03 '21 at 15:49
1

Alternative approach instead of doing grep recursively with -ir would be to let find 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 to grep. Excluding directories is done via -type f for finding only regular files.

$ find . -not -path "*.svn*" -type f -name "*.cpp" -or -name "*.h"  -readable -exec grep "my terms" "{}" \; 
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
0

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.com/search?q=ubuntu%20search%20inside%20installed%20packages

wjandrea
  • 14,109
  • 4
  • 48
  • 98
dargaud
  • 880
  • 3
  • 12
  • 24