How can I find the mime type without charset using "file"?

I am going to use "file" to check file type of a buch of files. I need to get it to print output file mime type without charset part.

My code:

file dog.jpeg -i

Output:

dog.jpeg: image/jpeg; charset=binary

What I want:

dog.jpeg: image/jpeg
0

2 Answers

From the man page:

 -i, --mime Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say ‘text/plain; charset=us-ascii’ rather than “ASCII text”.
--mime-type, --mime-encoding Like -i, but print only the specified element(s).

So by way of example:

$ file --mime dog.jpeg
dog.jpeg: image/jpeg; charset=binary
$ file --mime-type dog.jpeg
dog.jpeg: image/jpeg
$ file --mime-encoding dog.jpeg
dog.jpeg: binary

So you want file --mime-type dog.jpeg.

This may not be a clean way of doing but you can utilize the cut cmd.

file dog.jpeg -i | cut --delimiter=";" --fields=1

cut del fields

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like