MacOS Command Line – Use mdfind to Identify All Encrypted DMG

command linedmgmacosspotlight

Can someone suggest a clever way to identify all dmg that are encrypted?

My only idea is rather complicated. use mdfind '(kMDItemFSName=*.dmg)' and then test the response from hdiutil imageinfo and log the result of those that ask for a password. Thanks to the answer and comments, the following command runs all the dmg through hdiutil, but I can't figure out how to identify the ones that give an error. Any ideas?

mdfind -0 "kMDItemFSName=*.dmg" |xargs -0 -I{} -L 1 hdiutil imageinfo {}

Best Answer

At the present time on my local disk I have 88 .dmg files, three of which are encrypted. Before running the command line below I didn't know how many encrypted .dmg files I had and if any, where they were. So while the following command line may look convoluted nonetheless it should work as advertised.

Open Terminal and copy and paste the entire command line below, as is, into the Terminal then press Enter.

mdfind '(kMDItemFSName=*.dmg)' | while IFS= read -r line; do printf "$line " & hdiutil isencrypted "$line"; done > dmg_file_list; grep ': YES' dmg_file_list > encrypted_dmg_file_list; clear; cat encrypted_dmg_file_list

This will create two files, dmg_file_list and encrypted_dmg_file_list, and output the contents of the latter to the Terminal. The files can also be opened in a text editor.

The files will contain the fully qualified pathname of the .dmg files followed by a space and either encrypted: NO or encrypted: YES in the dmg_file_list file and only the fully qualified pathname of the .dmg files followed by a space and encrypted: YES in the encrypted_dmg_file_list file.

You can then manually delete the two files created by the command when finished with them.

Note: Once the command line is executed if may take a moment to process and output the contents of the encrypted_dmg_file_list file to the Terminal. It will depend on just how many .dmg files there are.

Here is the full command line shown with line continuation so you make sure to copy and paste the entire line. (You can actually copy and paste the command line in this format too.)

mdfind '(kMDItemFSName=*.dmg)' | while IFS= read -r line; \
do printf "$line " & hdiutil isencrypted "$line"; \
done > dmg_file_list; grep ': YES' dmg_file_list > encrypted_dmg_file_list; \
clear; cat encrypted_dmg_file_list