How can I know if a file is a binary file?
For example, compiled c file.
I want to read all files from some directory, but I want ignore binary files.
It's kind of brute force to exclude binary files with tr -d "[[:print:]\n\t]" < file | wc -c
, but it is no heuristic guesswork either.
find . -type f -maxdepth 1 -exec /bin/sh -c '
for file in "$@"; do
if [ $(LC_ALL=C LANG=C tr -d "[[:print:]\n\t]" < "$file" | wc -c) -gt 0 ]; then
echo "${file} is no ASCII text file (UNIX)"
else
echo "${file} is ASCII text file (UNIX)"
fi
done
' _ '{}' +
The following brute-force approach using grep -a -m 1 $'[^[:print:]\t]' file
seems quite a bit faster, though.
find . -type f -maxdepth 1 -exec /bin/sh -c '
tab="$(printf "\t")"
for file in "$@"; do
if LC_ALL=C LANG=C grep -a -m 1 "[^[:print:]${tab}]" "$file" 1>/dev/null 2>&1; then
echo "${file} is no ASCII text file (UNIX)"
else
echo "${file} is ASCII text file (UNIX)"
fi
done
' _ '{}' +