Select by File Type and Move Them to a New Folder

Hey everyone

I want to create a macro that selects certain file types within a folder (by extension), makes a new folder in that location and moves all selected files of that type to the new folder.

Moving all but the newest file of that type would be helpful too :slight_smile:

Optionally, it would be great to run it on a batch of folders at once, say, select the top folder and have it run in all sub folders if it finds the matching file types.nature.

Any suggestions?

Thanks in advance for any help on this :slight_smile:

If you do some searching in the forum you should find many examples very similar to your needs.
You can also take a look at Best Macro List -- KM Forum.

Hey @Five18,

You'd think this would be dead simple – right?


It's pretty tedious to set up, although once you have a template it's not bad.

Here's a relatively canonical way to do it.


Consolidate Files by File Extension v1.00.kmmacros (12 KB)

1 Like

That worked like a charm! Thank you!

1 Like

Hey @Five18,

With this macro I've used Keyboard Maestro native actions to make it easy for the user to adjust the file-extension and the destination folder name, but I've done the heavy lifting with the shell.

As I mention in the macro notes – do NOT play around with shell scripts without understanding what you are doing – there is the potential to destroy data on your hard drive. Leave the Execute a Shell Script action alone, unless you've got a reasonable grasp of shell scripting.


Consolidate Files by File Extension ⇢ Shell Version v1.00.kmmacros (11 KB)

In this version I've used the shell to do all the work.

Since I'm reasonably proficient with the shell this is less work for me than building the whole macro from Keyboard Maestro native actions.

I'm out of practice though, so building these has been good exercise for me.


Consolidate Files by File Extension ⇢ Shell Version v2.00.kmmacros (6.2 KB)

Wow. Thanks for all your hard work!

One more question... :slight_smile:
How would I alter it if i wanted more than one file type consolidated? I usually have 2 different file extensions that my audio program creates and I want to put them both in one folder.
Again thanks for all your help!

I tried to duplicate it and run it again with a different extension in the 'zip' line but it throws an error...

Consolidate Files by File Extension ⇢ Shell Version v2.00 edit.kmmacros (8.0 KB)

Your macro works perfectly fine for me, although it creates two different folders – one for each file extension.

I suspect you're having a problem with your trigger or your group activation.

Put the test macro in an always active global or Finder-only group, and give it a simple trigger like F1 for testing.

I'll get to this in a bit – I'll have to make the script a trifle less complex, which will make it a bit more complicated for an inexperienced user.


Here you go.


Consolidate Files by File Extension ⇢ Shell Version v3.00.kmmacros (6.2 KB)

Works perfectly! Thanks for your time and help with this! Happy new year!

How would you keep JUST the most recent file and move all others? Is it pretty simple cause I don't want to keep bugging you but that's the last thing I was hoping to do with KM. I thought if I could get this far I'd be able to get it on my own but I know zip about shell scripting :slight_smile:


It would only be 1 file type for this action though

Another option you have is to use Hazel, which is built specifically to monitor files and folders and then act on them based on criteria you set up. Many KM users also employ Hazel, and the two apps work well together.

I have several Hazel actions that move and/or rename files after a period of time. For instance, I take a lot of screenshots, so I have Hazel actions that keep screenshots I've taken in the last 48 hours on my desktop, but anything older gets moved to an archive and sorted into dated folders (year and month). Other types of files that land on my Desktop, Downloads, and dozens of other folders have similar do-something-if-file-is-older-than actions.

Thanks I'll look into it :slight_smile:

Hey @Five18,

Not simple with Keyboard Maestro or AppleScript – but doable.

Yyou wanted a version that left the most recent pair of files in place.

This was a bit tedious to do with vanilla AppleScript, but not too bad.

Easier to do with the shell I think, but I'm more familiar with AppleScript.


Consolidate Files by File Extension ⇢ AppleScript Version v1.00.kmmacros (8.7 KB)

Hazel is a great utility and muy powerful, but at $42.00 U.S. it is more expensive than Keyboard Maestro.

Nevertheless – if it flicks your bic then more power to you. Hazel is made for jobs just like this one.


Hey @Five18,

Okay. I've given you an AppleScript solution for leaving the most recent file-pair in place, but I couldn't resist doing the job in the shell for practice.

It was quite fiddly to get it running on Bash 3.2.57(1), which is stock on Sierra and still stock on Mojave! That silly thing goes back to 2007.

I have Bash 5.0.17(1) on my system – installed via MacPorts (although Homebrew is probably easier to use).

I wonder what's stock on Big Sur...

In any case – I've got this running on both my macOS 10.12.6 Sierra and my macOS 10.14.6 Mojave systems.

I've commented the Bash script reasonably thoroughly, but I haven't gone out of my way to make it easy for the user to modify.


Consolidate Files by File Extension ⇢ Ignore Most Recent Pair ⇢ Shell Version v4.00.kmmacros (11 KB)

Thanks for all of your hard work on this! I only needed the 1 file type (cpr) saved ion the most recent but I'm sure I can figure out how to alter it (hopefully haha)

Thanks again!

Well... Maybe not LOL

Hmm... I wonder how I got the idea we were dealing with .csh & .bak files...



Try this. It now operates only on .cpr files.

Consolidate .cpr Files by File Extension ⇢ Ignore Most Recent ⇢ Shell Version v4.01.kmmacros (11 KB)


Brilliant! This is exactly what I was looking for! Thank you!

1 Like