-2.4 C
New York
Saturday, December 6, 2025

macos – The right way to discover solely folders that container particular kind of merchandise


There are in all probability a number of methods to do that within the Terminal.  Right here’s one, which calls discover to seek for the pictures*, then dirname on every filename to extract the total listing identify, and eventually uniq to take away duplicates.

discover 'pathToLookUnder' -iname '*.jpg' -print0 | xargs -0 dirname | uniq

(Clearly, change pathToLookUnder with the trail to the folder you need to search in.  Or cd to it first, and use . as a substitute.)

That is structured as a pipeline: the filenames that discover lists are handed on to dirname (utilizing xargs to name it for each) and the outcomes then handed to uniq.

The ‘-print0’ and ‘-0’ flags work round issues with particular characters.  If any of the filenames comprises a quote or comparable, xargs would usually attempt to break up there.  To work round that, ‘-print0’ tells discover to separate filenames with a zero byte; and ‘-0’ tells xargs to separate solely on zero bytes.  So this could work for any filenames (that don’t comprise newlines).

This may return the folder names in an unspecified order.  You possibly can in fact append | type if you’d like them ordered.

The outcomes are listed on the terminal.  You possibly can as a substitute save them to a file, by appending >filename.txt.  Or you possibly can copy them to the clipboard by appending | pbcopy (to allow them to then be pasted into different apps).

(* You haven’t but specified the right way to establish the pictures.  For the sake of argument, this assumes all of them finish with .jpg.)

Related Articles

Latest Articles