7

For certain obvious usages, the grep command can output multiple complaints about search names being directories. Note the multiple "Is a directory" warnings, this typically happens with commands like: grep text *

eg:

/etc/webmin # grep "log=1" * 
grep: cluster-webmin: Is a directory
config:log=1
grep: cpan: Is a directory
grep: man: Is a directory
miniserv.conf:log=1
miniserv.conf:syslog=1
grep: mon: Is a directory

I know I can use the "-s" option to suppress these warnings about directories (and stderr can be redirected to nul, but that's even worse), but I don't like that because it's extra boilerplate which we have to remember every time, and it also suppresses all warnings, not just ones about directories.

Is there some way, where this spurious warning can be suppressed forever and globally ? I'm most interested in Debian and Cygwin.

1
  • The reason: grep don't know what to do with directories. Please tell him (with -d) to either recurse (read directories and subdirectories recursively) or skip
    – Ofer Rahat
    Commented Aug 30, 2022 at 8:45

3 Answers 3

15

Depending on the way you want to handle directories contents,

  • grep -d recurse will do it (handling recursively directories), or
  • grep -d skip (ignoring directories and their content).

You could have this be automatic, by adding it to ~/.profile or ~/.bashrc (one user) or /etc/profile or /etc/bashrc (all users)

alias grep="/bin/grep -d skip" 
2
  • This works nicely. I also found something called GREP_OPTIONS where the "-d skip" could be added instead. However, some bright spark somewhere decided that GREP_OPTIONS has been deprecated, and prints an annoying message when used, alas. So adding this as an alias (to profile or bashrc or whereever) is the best option. alias grep='/bin/grep -d skip'
    – William
    Commented Mar 21, 2018 at 19:22
  • @William - they'll remove GREP_OPTIONS due to problems it causes when writing portable scripts - hence the warning... Commented Mar 21, 2018 at 21:13
0

You could set an alias in your .profile or .bashrc:

alias grep='grep -s'

For global you could do it in /etc/profile or /etc/bashrc

2
  • I realize I can do this, and it would help with many cases of suppressing this warning. But, I am hoping for something which does not alter how grep is invoked/executed, and which won't suppress other grep warnings, and would work even for when grep isn't run (directly) by the shell.
    – William
    Commented Mar 20, 2018 at 13:40
  • As long as it's in your bashrc it will work from within scripts.
    – jesse_b
    Commented Mar 20, 2018 at 13:45
0

Just for completeness, GNU grep honours an environment variable called GREP_OPTIONS. From man grep:

GREP_OPTIONS
      This variable specifies default options to be placed in front of
      any explicit options.  As  this  causes  problems  when  writing
      portable  scripts,  this  feature  will  be  removed in a future
      release of grep, and grep warns if it is used.   Please  use  an
      alias or script instead.

You can do export GREP_OPTIONS=-s, but, as the manpage says, this will also get passed on to scripts and might unintentionally mess up things.

For example, I have had GREP_OPTIONS=--color=always mess up configure scripts used for building software. Debugging that was a pain, it wasn't at all clear where the problem was.

2
  • See the comments to tonioc's answer, for why your solution is undesirable. Commented Mar 22, 2018 at 6:04
  • @user1404316 please read the answer itself to see why this is undesirable.
    – Olorin
    Commented Mar 22, 2018 at 10:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .