Ubuntu – How to view binary .log files with application/octet-stream mime type

file typemime-type

I have downloaded some log files from my server which I want to search for a specific string for debugging purposes. All of them have the .log extension.

The problem is that one file has the plain text document (text/plain) mime type while the other has the Binary (application/octet-stream) mime type.

I can open the plain text document (text/plain) mime type log file in a text editor as plain text but the other I can't since it's in binary.

How can I view the binary .log files with application/octet-stream mime type?

enter image description here

Best Answer

From this answer in What makes grep consider a file to be binary?

If there is a NUL character anywhere in the file, grep will consider it as a binary file.

There might a workaround like this cat file | tr -d '\000' | yourgrep to eliminate all null first, and then to search through file.


I first tried it with one file in a test directory:

parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518
parto@subroot:~/Desktop/test$ cat info_pdslpostpaid.log-20160518 | tr -d '\000' > info_pdslpostpaid.log-20160518_edited
parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518  info_pdslpostpaid.log-20160518_edited
parto@subroot:~/Desktop/test$

And the result, a plain text document (text/plain) text mime file.

enter image description here

Then since I am working with multiple files, I tried running the same command for multiple files in a directory:

parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518  info_pdslpostpaid.log-20160520  info_pdslpostpaid.log-20160523  info_pdslpostpaid.log-20160525
parto@subroot:~/Desktop/test$ for i in * ; do cat "$i" | tr -d '\000' > "${i}_edited" ; done
parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518         info_pdslpostpaid.log-20160520_edited  info_pdslpostpaid.log-20160525
info_pdslpostpaid.log-20160518_edited  info_pdslpostpaid.log-20160523         info_pdslpostpaid.log-20160525_edited
info_pdslpostpaid.log-20160520         info_pdslpostpaid.log-20160523_edited
parto@subroot:~/Desktop/test$

And awesome, all my log files are now in a readable format!! :)