Using grep with /dev/null, an old Unix trick

February 12, 2019

Every so often I will find myself writing a grep invocation like this:

find .... -exec grep <something> /dev/null '{}' '+'

The peculiar presence of /dev/null here is an old Unix trick that is designed to force grep to always print out file names, even if your find only matches one file, by always insuring that grep has at least two files as arguments. You can wind up wanting to do the same thing with a direct use of grep if you're not certain how many files your wildcard may match. For example:

grep <something> /dev/null */*AThing*

This particular trick is functionally obsolete because pretty much all modern mainstream versions of grep support a -H argument to do the same thing (as the inverse of the -h argument that always turns off file names). This is supported in GNU grep and the versions of grep found in FreeBSD, OpenBSD, NetBSD, and Illumos. To my surprise, -H is not in the latest Single Unix Specification grep, so if you care about strict POSIX portability, you still need to use the /dev/null trick.

(I am biased, but I am not sure why you would care about strict POSIX portability here. POSIX-only environments are increasingly perverse in practice (arguably they always were).)

If you stick to POSIX grep you also get to live without -h. My usual solution to that was cat:

cat <whatever> | grep <something>

This is not quite a pointless use of cat, but it is an irritating one.

For whatever reason I remember -h better than I do -H, so I still use the /dev/null trick every so often out of reflex. I may know that grep has a command line flag to do what I want, but it's easier to throw in a /dev/null than to pause to reread the manpage if I've once again forgotten the exact option.


Comments on this page:

By Christopher Barts at 2019-02-13 02:45:14:

Instead of

   cat <foo> | grep <bar>

I'd do

   grep <bar> < <foo>

That is, do redirection. I find the placement of the input to be nicely mnemonic given which way the redirection points, and you can still redirect or pipe output as normal.

This seems to be POSIX-compatible:

http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html

By cks at 2019-02-13 08:39:15:

I should have been clearer. In this particular situation, redirection doesn't work because we are specifically cat'ing multiple files. If we only had one file, 'grep <thing> file' would work fine to not print the filename; it is only with multiple files where we need to present grep with a single 'file', hence:

cat file1 file2 ... | grep <thing>

There is no way to do this with redirection.

Written on 12 February 2019.
« Thinking about the merits of 'universal' URL structures
An unpleasant surprise with part of Apache's AllowOverride directive »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue Feb 12 23:40:09 2019
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.