How to Find the Most Recent File of a Given Type in a Given Directory Tree

Hey Chris. Hope you had a good New Year.

Couple of questions about this:

  1. Is it possible to narrow down the results by generalised filetype, the way that Finder does? I'd like to find, for example, the latest movie file or audio file within subfolders.

  2. How do I stop the macro from displaying the results in a window and pass it to a variable instead?

Hey Neil,

Q1 – is it possible?

Yes – you can use Spotlight from the command line:

#!/usr/bin/env bash

cd /Users/chris/test_directory/;

mdfind -0 -onlyin . 'kMDItemContentTypeTree == "public.text*"c' \
| xargs -0 stat -f "%m %N" \
| sort -rn \
| head -n 1 \
| sed -E 's!^[^ ]+ !!'

Q2 – take a good look at the Execute a Shell Script action, and then you tell me.


Hi Chris,

Thanks for that. I'll have to dedicate some time to learning about shell scripts.

All the best,


Hey Neil,

All of the execute script actions have a number of options.

Take note of the pop-up menu for the disposition of the script result (second from the left):

cd /Users/chris/test_directory/;



Here's a working example:

Extract the Most Recently Modified File of a Given Type from a Given Folder Path v1.00.kmmacros (7.9 KB)



Hi Chris,

I really appreciate that, and can only apologise for my ignorance; it's not sheer laziness, I promise!

It works perfectly when the chosen directory is something like the Downloads folder or a folder on the Desktop, but I've noticed that it doesn't work for temporary cache files in, for example "/private/var/folders/6h/4b39kdzj1rbgwln1rpklhm780000gn/T/". Is that a permissions thing, do you think?


What kind of file are you searching for?

I placed a movie file in a desktop folder and that worked fine. I moved that same file to the aforementioned "/private/var..." directory to test and it didn't find it. By "temporary cache files", I still meant movie files, that are temporarily stored by streaming software.

This is super useful though, regardless. Thanks!

Hey Neil,

This is where I rant about Apple's inability to get the details right...

You can go to /private/var/folders/ in the Finder and search for:


In the Spotlight search field in the front Finder window.

You'll find your movie.

But if you try to use Spotlight from the command line you won't find it because the metadata isn't there:

kMDItemFSContentChangeDate = 2022-01-15 23:08:30 +0000
kMDItemFSCreationDate      = 2021-07-11 01:37:21 +0000
kMDItemFSCreatorCode       = ""
kMDItemFSFinderFlags       = 0
kMDItemFSHasCustomIcon     = 0
kMDItemFSInvisible         = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSIsStationery      = 0
kMDItemFSLabel             = 0
kMDItemFSName              = "TEST_MOVIE copy.mp4"
kMDItemFSNodeCount         = 1694501
kMDItemFSOwnerGroupID      = 20
kMDItemFSOwnerUserID       = 501
kMDItemFSSize              = 1694501
kMDItemFSTypeCode          = ""

HoudahSpot which leverages Spotlight can also find within that directory, although I don't know what magic it uses to do so.

Using the Unix find command line utility requires sudo to find everything effectively.

I can get the job done with either the shell or with AppleScript if I want to work hard enough, but I don't fancy the trudge.

My best advice for now is to use the Finder's Spotlight search.

You can turn on date-added in the view of the front Finder window, and sort with it.


Hey Chris,

Noted; I'll stick to Finder for that. You're so knowledgeable. I realise I ask a lot of questions, and I really appreciate your patience and kindness. Thankyou!


1 Like