Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which command can be used to determine if a file is binary

Tags:

linux

bash

ubuntu

I got some issues grepping a certain text in a folder.

grep "Notifying status" -R
Binary file fix2.log matches

It turns out that the file containing the text is binary. Actually it is a log file (created by PAC manager) and can be read properly using notepad++ (or any other text file). I'm not sure why the OS determines it a

When I do ls so all files are listed with the same -rwxr--r--

When I do file fix2.log it shows as data

Is there any command to show if a file is binary or not, example from ls view?

like image 731
artm Avatar asked Aug 02 '16 01:08

artm


1 Answers

Under Linux/Unix, look for anything that isn't "text/plain":

$ file -I -b /tmp/local-access.log
text/plain; charset=us-ascii

Edit:

Actually, since file does classify between different types of text-file, the encoding might be the better way to go:

$ echo "Testing" | file -I -b -
text/plain; charset=us-ascii

$ echo "<html></html>" | file -I -b -
text/html; charset=us-ascii

$ echo "<?xml version="1.0"?><catalog></catalog>" | file -I -b -
application/xml; charset=us-ascii

$ echo "<?xml version="1.0"?><catalog></catalog>" | file --mime-encoding -b -
us-ascii

Note @CharlesDuffy's comment below. This will only check some head/tail information but, 1) this will be sufficient in all non-exotic situations (most binary files aren't going to have pure text at the front and back of the file), and 2) you don't necessarily want to check every byte if the input is of arbitrary length (e.g. 2G)

like image 110
Dustin Oprea Avatar answered Oct 18 '22 20:10

Dustin Oprea