grep for ascii nul

Ronald F. Guilmette rfg at
Fri Nov 1 20:07:09 UTC 2019

In message <20191101092716.GA67658 at>, 
Victor Sudakov <vas at> wrote:

>I need to find files containing ascii null inside, and print their names to
> stdout.

Unfortunately, you're banging up against a long-standing a rather
annoying non-feature of fgrep/grep/egrep, which is that unlike the
tr command, the grep family of commands does not support the \DDD
notation for specifying arbitrary byte values.  Thus, you cannot use
then to search for arbitrary byte values.

I would thus suggest that you solve your problem using a Perl or C
program.  It would be relatively easy to code up a solution to your
stated problem in either of these two languages.

Something approximately like this might work.  (But I DO NOT guarrantee
that this will work.  If you want guarrantees, send money.)  You would
give this script a list of the filenames you wanted checked on the
command line when you invoke it.

#!/usr/local/bin/perl -w
use strict;
undef $/;

foreach my $arg (@ARGV) {
  open (IFILE, "<arg") || die;
  my $content = <IFILE>;
  close (IFILE) || die;
  print "$arg\n" if ($content ~= m/\000/);

More information about the freebsd-questions mailing list