macOS - Dealing with Packages (script)

This is a simple bash script to shorten the commands I frequently use to deal with installed packages (.pkg).


  macos [~] $ pkg

   Usage: pkg [option] package.name.pkg

   Options: -l  list all installed packages
            -i  show package info
            -a  list package files and directories
            -f  list package files only
            -d  list package directories only
            -r  remove package files
            -o  open receipts location

The script makes use of the pkgutil to perform the tasks above. Here is pkgutil's help:


  macos [~] $ pkgutil --help
Usage: pkgutil [OPTIONS] [COMMANDS] ...

Options:
  --help                 Show this usage guide
  --verbose, -v          Show contextual information and format for easy reading
  --force, -f            Perform all operations without asking for confirmation
  --volume PATH          Perform all operations on the specified volume
  --edit-pkg PKGID       Adjust properties of package PKGID using --learn PATH
  --only-files           List only files (not directories) in --files listing
  --only-dirs            List only directories (not files) in --files listing
  --regexp               Try all PKGID arguments as regular expressions

Receipt Database Commands:
  --pkgs, --packages     List all currently installed package IDs on --volume
  --pkgs-plist           List all package IDs on --volume in plist format
  --pkgs=REGEXP          List package IDs on --volume that match REGEXP
  --groups               List all GROUPIDs on --volume
  --groups-plist         List all GROUPIDs on --volume in plist format
  --group-pkgs GROUPID   List all PKGIDs in GROUPID
  --files PKGID          List files installed by the specified package
  --lsbom PKGID          List files in the same format as 'lsbom -s'
  --pkg-groups PKGID     List all GROUPIDs that PKGID is a member of
  --export-plist PKGID   Print all info about PKGID in plist format
  --pkg-info PKGID       Show metadata about PKGID
  --pkg-info-plist PKGID Show metadata about PKGID in plist format
  --file-info PATH       Show metadata known about PATH
  --file-info-plist PATH Show metadata known about PATH in plist format
  --forget PKGID         Discard receipt data for the specified package
  --learn PATH           Update --edit-pkg PKGID with actual metadata from PATH

File Commands:
  --expand PKG DIR       Expand the flat package PKG to DIR
  --flatten DIR PKG      Flatten the files at DIR as PKG
  --bom PATH             Extract any Bom files from the pkg at PATH into /tmp
  --payload-files PATH   List the paths archived within the (m)pkg at PATH
  --check-signature PATH Validate the signature of the pkg at PATH and print certificate information

Script

#!/usr/local/bin/bash

case "$1" in

  -l)
    pkgutil --pkgs
    ;;

  -i)
    pkgutil --pkg-info $2
    ;;

  -a)
    pkgutil --files $2
    echo
    pkgutil --pkg-info $2
    ;;

  -f)
    pkgutil --only-files --files $2
    pkgutil --pkg-info $2
    ;;

  -d)
    pkgutil --only-dirs --files $2
    pkgutil --pkg-info $2
    ;;

  -r)
        cd /
    sudo pkgutil --only-files --files $2 | tr '\n' '\0' | xargs -o -n 1 -0 sudo rm -v
    sudo pkgutil --forget $2
    ;;

  -o)
        open /private/var/db/receipts
        ;;

  *)
    echo
    echo "   Usage: pkg [option] package.name.pkg"
    echo
    echo "   Options: -l  list all installed packages"
    echo "            -i  show package info"
    echo "            -a  list package files and directories"
    echo "            -f  list package files only"
    echo "            -d  list package directories only"
    echo "            -r  remove package files"
    echo "            -o  open receipts location"
    echo
    ;;
esac
{{ message }}

{{ 'Comments are closed.' | trans }}