Find files > 0 bytes in length that consist only of NUL in Debian/Ubuntu
Assuming your files contain ONLY the NULL string (no new line, \n
), you can use grep
and find to locate these files.
The find command will collect all files that are at least 1 byte in size (-size +1c
) in the current directory and then use grep to check if they contain only repetitions of the NULL character:
$ find . -type f -size +1c -exec grep -m 1 -ovP "[^\0]" {} \;
Binary file ./empty_file with spaces.jpg matches
Binary file ./empty_file matches
The -v
flag of grep causes it to print non matching lines. -m
1 means "Stop after the first match", -o
means print only the part of the line that matches (avoids printing empty lines from other files) and P
uses Perl regular expressions. The pattern [^\0]
, means any non NULL
character. By reversing this (-v
) and adding the -m 1
flag, grep will report a match only if the file contains nothing but NULL
.
Finally, you can pipe this through cut
and sed
to print the name of the matching file alone:
$ find . -type f -size +1c -exec grep -m 1 -ovP "[^\0]" {} \; | cut -d ' ' -f 3- | sed 's/ matches//'
./empty_file with spaces.jpg
./empty_file