A common issue faced by Mac admins is that users can have multiple copies of an application on their system. This can be the result of users having standard user rights and running applications out of the home directories, or users reorganizing the Applications directory in a way that makes more sense to them for their needs. Meanwhile, when Mac admins need to report on whether the apps on the fleet are up to date, these additional or reorganized apps may not be patched but do show up in the software reporting as being out of date.
When you run into these situations, how to handle them? The first step is finding where the duplicates are and reporting on them. To do this, you can use the mdfind command line tool, using the kMDItemCFBundleIdentifier metadata attribute. This metadata attribute reports on the bundle identifier used by an application, so it should pick up all copies of an app which are using that unique identifier for that app. For more details, please see below the jump.
As an example case, I’m going to use the following scenario:
- I need to locate all copies of the BBEdit app installed on a particular Mac
- The user in question has three copies of BBEdit installed.
- In /Applications
- In a user-created BBEdit directory inside of /Applications.
- In a user-created Applications directory inside of their home folder.
BBEdit’s bundle identifier is com.barebones.bbedit, so I can use the following mdfind command to find all copies of BBEdit.app:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.barebones.bbedit'" |
In the scenario described above, that should provide output similar to what’s shown below:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username@computername ~ % /usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.barebones.bbedit'" | |
/Applications/BBEdit/BBEdit.app | |
/Users/username/Applications/BBEdit.app | |
/Applications/BBEdit.app | |
username@computername ~ % |
However, you may not necessarily want to know about /Applications/BBEdit.app as that may be the only version you want installed. To exclude /Applications/BBEdit.app from your results, you can use the grep command line tool’s -v inverse match and -E regular expression options to exclude /Applications/BBEdit.app from the list of results:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username@computername ~ % /usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.barebones.bbedit'" | grep -vE "^/Applications/BBEdit.app" | |
/Applications/BBEdit/BBEdit.app | |
/Users/username/Applications/BBEdit.app | |
username@computername ~ % |
If you needed to exclude additional directories (for example, you must exclude searching the user’s home folder for privacy reasons), you can add additional regular expressions to the grep command to exclude additional undesired results. Here’s an example where you’re excluding results from /Users and for /Applications/BBEdit.app:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username@computername ~ % /usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.barebones.bbedit'" | grep -vE "^/Users|^/Applications/BBEdit.app" | |
/Applications/BBEdit/BBEdit.app | |
username@computername ~ % |
For our scenario, here’s a script that should identify all installed copies of BBEdit on a Mac and display a list of all copies except for /Applications/BBEdit.app:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
appname="BBEdit" | |
appidentifier="com.barebones.bbedit" | |
/usr/bin/mdfind "kMDItemCFBundleIdentifier == '$appidentifier'" | grep -vE "^/Applications/$appname.app" |