How to get a file's UTI from the command line in Mac OS X?

24

6

Is there a command line utility to which I can pass a filename, that will return applicable UTIs for that file?

For example:

hypothetical-uti-fetcher /Library/Desktop\ Pictures/Aqua\ Blue.jpg

might give me:

public.jpeg
public.image
public.data

smokris

Posted 2010-11-10T00:10:51.270

Reputation: 1 352

Answers

28

You can use the mdls command to find the UTI for a file:

mdls /Library/Desktop\ Pictures/Aqua\ Blue.jpg

and it will give you a bunch of info, toward the top it will say something like

kMDItemContentType             = "public.plain-text"

Hope this helps!

Wuffers

Posted 2010-11-10T00:10:51.270

Reputation: 16 645

1Suppose I turned off my Spotlight index and I seem to have no metadata stored. (mdls whatever-file just says that the file was not found). How do the md* services generate the content types in the first place? – Aurel Bílý – 2015-02-17T17:53:41.477

3Excellent. Thanks. mdls -name kMDItemContentTypeTree /Library/Desktop\ Pictures/Aqua\ Blue.jpg is exactly what I wanted. – smokris – 2010-11-10T00:41:13.213

You're welcome, I'm glad this helped. – Wuffers – 2010-11-10T01:06:30.063

7

The mdls command only seems to work on Spotlight indexed files. If you run it on a mounted disk image, cd or thumb drive it may return no information at all.

When you're developing a Quicklook Plugin it's also useful to use the following command to see what UTIs Quicklook thinks a file has and which plugins it uses for the preview:

qlmanage -d 4 -p /path/to/file

Karsten

Posted 2010-11-10T00:10:51.270

Reputation: 171

The cited site is no longer available. – Randall – 2019-01-15T10:36:13.783