Skip to content

Instantly share code, notes, and snippets.

@ernstki
Created April 19, 2024 16:58
Show Gist options
  • Save ernstki/25d4492748b696ae42956cb001262c1b to your computer and use it in GitHub Desktop.
Save ernstki/25d4492748b696ae42956cb001262c1b to your computer and use it in GitHub Desktop.
Wrapper script for macOS's `pkgutil` to list full pathnames of files in all packages, or packages matching a pattern
#!/bin/sh
pkgutil --packages \
| if [[ "$1" ]]; then grep -i "$1"; else cat; fi \
| perl -nE '
#line 5 "pkgutil-list-files"
chomp;
print STDERR "# scanning $_…\n";
$location = `pkgutil --pkg-info $_`;
$location =~ s/.*location: (.*?)\n.*/$1/s;
print STDERR "# location: $1\n";
print STDERR "# running \`pkgutil --files $_\`";
open $fh, "-|", "pkgutil --files $_";
while ($path = <$fh>) {
$pkgfile = $location ? "/$location/$path" : "/$path";
# `if -f $pkgfile` doesn’t work here for some reason :(
print "$_\t$pkgfile";
}
close $fh;
'
@ernstki
Copy link
Author

ernstki commented Apr 19, 2024

The first column of the output (tab-separated) is the package name. If you are brave enough to xargs rm the output of this command, then pipe through cut -f2 first to get the second column.

Remove or comment out print STDERR statements if it's too chatty for you, or simply pkgutil-list-files 2>/dev/null to suppress them for a single run.

See also: https://stackoverflow.com/questions/25925752/uninstall-packages-in-mac-os-x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment