With GNU grep (and several other grep implementations), you can search for files that do not contain any printable character. The
-L option means to list the files that do not contain a match.
[[:print:]] (yes, there are two pairs of brackets) matches one printable character; the definition of printable character depends on your locale.
grep -L '[[:print:]]' -- *
To delete them all (review the
grep output first to make sure you're deleting the right files):
grep -L '[[:print:]]' -- * |
while IFS= read -r filename; do rm "$filename"; done
set +f; IFS='
' # split at newlines, turn off globbing
rm -- $(set -f; grep -L '[[:print:]]' -- *)
set -f; unset IFS
Note that the commands above will produce error messages if there are subdirectories in the current directory (because you'll be calling
rm on a directory — don't call
rm -r!). In zsh, you can use
*(.) instead of
* to only match regular files, and you don't need to worry about globbing:
rm -- $(grep -L '[[:print:]]' -- *(.))
With only POSIX tools,
grep -l '[[:print:]]' -- * shows the files you want to keep.
Or you can iterate over the files; this is clearer, but theoretically (but probably not measurably) slower.
for x in *; do
if [ -f "$x" ] && ! grep -q '[[:print:]]' <"$x"; then
rm -- "$x"