3

Using exclusively terminal, how can one identify and delete the expired provisioning profiles from ~/Library/MobileDevice/Provisioning Profiles

Is there a way to do that just from terminal?

nacho4d
  • 43,720
  • 45
  • 157
  • 240
Duck
  • 34,902
  • 47
  • 248
  • 470
  • I set bash tag instead of ios-provisioning for the sake of correct syntax highlighting of the answer – nacho4d May 12 '20 at 16:27

1 Answers1

11

You can write a shell script that will loop through the files, grab the date from the mobileprovision file, and check it against the current date.

#!/bin/sh

for provisioning_profile in ~/Library/MobileDevice/Provisioning\ Profiles/*.mobileprovision;
do
    printf "Checking ${provisioning_profile}... "

    # pull the expiration date from the plist
    expirationDate=`/usr/libexec/PlistBuddy -c 'Print :ExpirationDate' /dev/stdin <<< $(security cms -D -i "${provisioning_profile}")`

    # convert expirationDate and current date to epoch (Unix Timestamps) then compare both.
    timestamp_expiration=`date -jf"%a %b %d %T %Z %Y" "${expirationDate}" +%s`
    timestamp_now=`date +%s`

    if [ ${timestamp_now} -ge ${timestamp_expiration} ];
    then
        echo "EXPIRED"
        # rm -f "${provisioning_profile}"
    else
        echo "not expired"
    fi

done

You can use the security command and plist buddy to extract the ExpirationDate from the file. Then for simplicity I just convert that date to a easily comparable format (YYYMMDD unix time stamps or seconds since 1970) and compare it to today's date in the same format. I print out the status of each. Note: I do not do the delete, because I want you to verify the script results before you uncomment the removal line. I ran it on mine, and threw in an old profile. It correctly identified the expired profile in my tests.

nacho4d
  • 43,720
  • 45
  • 157
  • 240
wottle
  • 13,095
  • 4
  • 27
  • 68
  • B R I L L I A N T ! ! ! Thanks – Duck May 24 '16 at 16:58
  • 1
    just a little touch to your script, the line `rm -f ${provisioning_profile}` should be in fact `rm -f "${provisioning_profile}"` because the provisioning profile file contains spaces and without quotes the file will not be deleted. – Duck Jul 05 '16 at 21:21
  • 1
    Yes, most, when installed through Xcode won't have spaces in there, but I did update it in case people manually copy them in or use something like fastlane. Thanks for following up! – wottle Jul 05 '16 at 21:26
  • I use fastlane but even the path `~/Library/MobileDevice/Provisioning Profiles` contains a space that will break the script... – Duck Jul 05 '16 at 21:32
  • 1
    Excellent script. I just modified edited a bit to make comparison of dates really exact. Before provisioning profile expiration date timezone was not taken into account. Also it was comparing only YYYMMDD. New version of the script takes account time zones and compare even seconds. – nacho4d May 12 '20 at 16:33
  • Nice. I never managed my provisioning profiles quite so closely, but appreciate the improvements. – wottle May 27 '20 at 22:17