简体   繁体   中英

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

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?

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 (eg 2G)

grep is simply looking for non-ASCII content for its "binary" determination. You can trivially override this with the -a flag, to assume that all content is text:

grep -a "Notifying status" -R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM