Using the mdfind command line tool to find duplicate copies of an application on macOS Sequoia
文章介绍了一种使用`mdfind`命令行工具查找macOS系统中应用程序重复副本的方法。通过`kMDItemCFBundleIdentifier`元数据属性,管理员可以快速定位具有相同bundle identifier的应用程序副本,并排除不需要的结果以简化管理流程。 2025-7-28 20:15:21 Author: derflounder.wordpress.com(查看原文) 阅读量:9 收藏

Home > Mac administration, macOS, Scripting > Using the mdfind command line tool to find duplicate copies of an application on macOS Sequoia

Using the mdfind command line tool to find duplicate copies of an application on macOS Sequoia

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:

  1. I need to locate all copies of the BBEdit app installed on a particular Mac
  2. 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:


/usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.barebones.bbedit'"

In the scenario described above, that should provide output similar to what’s shown below:


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:


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:


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:


#!/bin/bash
appname="BBEdit"
appidentifier="com.barebones.bbedit"
/usr/bin/mdfind "kMDItemCFBundleIdentifier == '$appidentifier'" | grep -vE "^/Applications/$appname.app"

文章来源: https://derflounder.wordpress.com/2025/07/28/using-the-mdfind-command-line-tool-to-find-duplicate-copies-of-an-application-on-macos-sequoia/
如有侵权请联系:admin#unsafe.sh