How do I test whether that is a binary file?

Hi, I tried locating my book for the keyword to test whether the file is a binary file but I can't find it under test.

Anyone any idea what the key letter is ?:frowning:

in solairs you can use the command file.

opps. forgot to mention I am using Bourne Shell.sorry!

Did you try:

$ type -type <file>

I know it will say file / builtin / hashed, etc.. I'm not sure about binary, that's bash too, i never tried it in sh...just a thought

If you mean binary in the unix sense (no carriage control)
use the command "file", and it is not some type of ELF or a.out format file:

file filename for a "binary" file
returns

filename: data

If file finds carriage control (newlines) and printable ascii characters:

filename: ascii data

Otherwise, it uses a magic number to figure out what type of file you are dealing with.

On PC's under DOS or Windows, binary files do not have exactly the same meaning they do for unix, nor does the file system handle them the same way. The EOF marker for text files is control-Z, ASCII 26. Binary files keep the length of the file in the FAT, and treat ascii 26 as an ordinary character. Unix does not do this. There is no EOF marker in the file. Binary and ascii files are handled the same way by the file system. It keeps track of where the file ends.

The file command will tell you the type of file - most of the time!

Because it is using the contents of the file it can sometimes get the answer wrong.

A different way of looking for a file type is to use the find command with the -type conjunction.

Also certain file types have a disctinct 'marker' in the data. e.g. some TIF files will have 0x490x49 as the first two bytes. Although you could write a program to look for this TIF marker, it would be very easy to fool it with a hoax file. And now, I hope you will appreciate the limitations with the file command.

mbb -

In unix a file is a bag of bytes. Period. There is no such thing as a binary file or a text file, except perhaps in terms of how you retreive data from it, or which magic number the file has. All I/O uses base modules like read. The higher-level i/o modules call read, then play with the data in the buffer and return chunks of it.

Windows programmers insist on "binary". When they come to linux it takes them along time to figure out what the file system actually does.

jim -

Yes thanks I know this!

But, the file command under HP-UX will come back with more than just 'data' or 'ascii data'. It will take a stab at naming the file type. e.g.

rmfiles: commands text
ls: PA-RISC1.1 shared executable dynamically linked

So under HP-UX at least, it may be possible to determine the type of file - I have seen scripts which attempt this.

As you say there is no 'type'* of file in Unix which is why the file command relies on file contents. This is why the file command is not reliable and may catch some programmers out!

  • The OS must recognise some internal marker for an executable, rather than an interpreted file right?

i cant see why file wouldnt suite your needs. maybe you are using a different version than i? from my file man page: