Reputation: 2273
Have a situation where I need a shell or bash script to determine if a file is binary or not. The issue here is that the linux environment does not have file
available and the grep
version is from busybox which doesn't support -I
. I found a perl
method (the perl version is old supports -e but not -E) of it working but it's slow. Does anyone have a faster method of determining if a file is binary? TIA!!
#!/bin/sh
is_text_file() {
# grep -qI '.' "$1" ### busy box grep doesn't support
perl -e 'exit((-B $ARGV[0])?1:0);' "$1" ### works but is slow
}
do_test_text_file_on_dir() {
for f in "$1"/*; do
[ -f "$f" ] || continue
if is_text_file "$f"; then
echo "$f" is not a binary file
fi
done
}
do_test_text_file_on_dir ~/testdir
Upvotes: 2
Views: 624
Reputation: 385647
Avoid the time it takes to repeatedly load perl
by doing all the work in Perl.
#!/usr/bin/perl
for (@ARGV) {
stat($_)
or warn("Can't stat \"$_\": $!\n"), next;
-f _ && !-B _
or next;
print("\"$_\" isn't a binary file\n");
}
Usage:
do_test_text_file_on_dir ~/testdir/*
Note: !-B _
is equivalent to -T _
except for empty files.
Upvotes: 3