Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the file is a binary file and read all the files which are not?

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.

like image 579
Refael Avatar asked May 26 '13 14:05

Refael


People also ask

How can you tell if a file is binary?

We can usually tell if a file is binary or text based on its file extension. This is because by convention the extension reflects the file format, and it is ultimately the file format that dictates whether the file data is binary or text.

Can binary files be read?

Binary files are not human-readable because the bytes they contain translate to characters and symbols that have many other non-printable characters. The text editor shows any binary file as characters such as Ø and ð. Binary files must be read by programs to be useable.

Which method is used to read data from a binary file?

The BinaryReader class is used to read binary data from a file. A BinaryReader object is created by passing a FileStream object to its constructor.


1 Answers

Use utility file, sample usage:

 $ file /bin/bash  /bin/bash: Mach-O universal binary with 2 architectures  /bin/bash (for architecture x86_64):   Mach-O 64-bit executable x86_64  /bin/bash (for architecture i386): Mach-O executable i386   $ file /etc/passwd  /etc/passwd: ASCII English text   $ file code.c  code.c: ASCII c program text 

file manual page

like image 200
Adam Siemion Avatar answered Sep 19 '22 19:09

Adam Siemion