Thursday, August 28, 2025

macos – discover solely folders that container particular kind of merchandise

There are most likely a number of methods to do that within the Terminal.  Right here’s one, which calls discover to seek for .jpg recordsdata, then dirname on every filename to extract the total listing title, and at last uniq to take away duplicates.

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

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

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

The ‘-print0’ and ‘-0’ flags work round issues with particular characters.  If any of the filenames incorporates a quote or related, 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 include newlines).

It will return the folder names in an unspecified order.  You’ll be able to after all append | type if you need them ordered.

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


UPDATE: -iname '*.jpg' will discover all recordsdata ending in ‘.jpg’ (or ‘.JPG’, or any mixture).  To seek for a number of extensions, you may as an alternative use a situation like:

'(' -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' ')' 

(The parens make sure that the next ‘-print0’ will apply to all of them.  And the quotes stop what’s within the parens being interpreted as instructions for a subshell, or file attributes, relying on the shell.)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles