1

Normally file command in linux provides the true file type of the file regardless of whether the extension is mentioned or not.

For ex:

# file cntlm-0.92.3-1.x86_64.rpm
cntlm-0.92.3-1.x86_64.rpm: RPM v3.0 bin i386/x86_64 cntlm-0.92.3-1

# file zip-pack
zip-pack: Zip archive data, at least v2.0 to extract

This gives a long description of file format, but I need only the format ( if it is a zip, it should print only zip)

How can this be achieved?

1 Answer 1

6

As you have noticed, the output of file is, by default, a bit verbose:

$ file some.pdf
some.pdf: PDF document, version 1.4

The -b option suppresses printing the file name and that helps:

$ file -b some.pdf
PDF document, version 1.4

If that is too much information, then use the -i option to print just the mime type and encoding:

$ file -bi some.pdf
application/pdf; charset=binary

Or, if you just need the type, use --mime-type:

$ file -b --mime-type some.pdf
application/pdf

A list of standard mime types can be found here. The word before the slash can be application, audio, video, or image, among others. If you want to eliminate that first word from the mime type standard, cut can be used:

$ file -b --mime-type some.pdf | cut -d/ -f2-
pdf

Since removing the first word from the mime type might leave some ambiguity, it is probably best to keep the full mime type.

1
  • 1
    Awesome answer :) Learned something new today.
    – FELDAP
    Sep 7, 2016 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .