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?
2 Answers
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' | yourgrepto 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.
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!! :)
Based on your own answer, you seem to be referring specifically to searching files using grep, rather than to changing a file's mime-type - see What is the XY problem?.
If grep is simply misidentifying the files based on null bytes, then you can use the -a or --binary-files=text options to tell grep to treat them as text regardless, as described in the manual pages:
-a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
--binary-files=TYPE If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is binary, and grep normally outputs either a one-line message saying that a binary file matches, or no message if there is no match. If TYPE is without-match, grep assumes that a binary file does not match; this is equivalent to the -I option. If TYPE is text, grep processes a binary file as if it were text; this is equivalent to the -a option. Warning: grep --binary-files=text might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands. 1