Unpacking an OS X pkg file

Mac Package Icon

“Back in the day” Mac pkg files simply used to be folders that you could right-click and “Show Contents” or even just cd into from the command line. This is still the case for Mac applications that you just drag and drop into the Applications folder but, more recently, the format installation packages seems to have changed, making getting into the guts of them a bit harder.

The newer pkg files are now just that; an individual file with all of the contents and installation scripts all compressed and wrapped up obscurely. After poking around in Google and playing around I’ve managed to figure out what’s needed to get to the goodies locked away inside. The pkg file itself is an “xar” archive – something that’s pretty new to me, but relatively easy to unpack:

MBP:unpack shiv$ xar -xf Package.pkg
MBP:unpack shiv$ ls
Distribution Package.pkg  Resources    root.pkg
MBP:unpack shiv$  cd root.pkg
MBP:root.pkg shiv$  ls
Bom         PackageInfo Payload
MBP:root.pkg shiv$

It spits out a few files and folders, but you are interested in getting to the location containing the “Payload” file. This file itself is a “cpio archive compressed with gzip” according to what I could dig up online. You can unpack it with a few commands piped together.

MBP:root.pkg shiv$ cat Payload | gzip -d - | cpio -id
2984 blocks
MBP:root.pkg shiv$ ls
Bom         PackageInfo Payload     private     usr
MBP:root.pkg shiv$

The new folders and files that appear are what (and where) contents would be installed, (usually) relative to the root directory of the hard drive. In my case, I was interested in getting to ./usr/local/bin to get to some binaries that I wanted to play with without other stuff being installed into my system without a clear way of removing them.

Great success!


  • 19/03/2013 - 8:58 AM | Permalink

    Thank you sooooooooooooooo much!

  • hilli
    26/08/2013 - 9:20 AM | Permalink

    Yes, indeed. Thanks a lot man!

  • Pingback: Using OS X's font validator

  • 29/11/2013 - 8:12 PM | Permalink

    You really just made my day. I was able to get a driver that was intentionally made to not install with my older version of some audio software working in seconds.

  • Barry
    30/10/2015 - 7:19 AM | Permalink

    Perfect!!! I needed to run a binary manually on the command line, because damn pkg installer checks for the OSX version and prevents installation. In my cause Airport Utility 5.6 on newer Mountain Lion. Thank You!

  • Leave a Reply

    Your email address will not be published. Required fields are marked *