Naming and renaming utilities

How to remove trailing spaces from filenames on Mac

Trims any space sitting right before the extension, for example Report .pdf becomes Report.pdf

Beginner · About 1 minute

In this guide

What this does

A stray space right before a file’s extension is easy to miss and often comes from copying text out of an email or a document title. This rule trims exactly that: any space sitting right before the extension, so “Report .pdf” becomes “Report.pdf”, without touching spaces anywhere else in the name.

What you’ll build

A rule that matches every real file, then trims any trailing space right before its extension.

Open the rule editor

This walkthrough happens inside FilesAutomator itself, so install it from the Mac App Store first if you have not already, then open it. In its toolbar, choose Add, then New Automation Rule.

Name the rule and choose a folder

Name it something like “Remove trailing spaces from my filenames” and use Add Folder to choose the folder you want this applied to.

Build the condition

Add a single condition: Size is greater than 0, matching any real file.

FilesAutomator
The Automation Rule Name card, alongside a single condition, Size is greater than 0
The Automation Rule Name card, alongside a single condition, Size is greater than 0

Add the action steps

Step 1: Rename

Choose Add Step, then Rename. In “New name, built from the parts below”, use Add Part, then Find and Replace in Name. Turn on “Find is a pattern” and enter this pattern to find:

\s+$

Leave the replacement empty. Add Part again, choose Text, type a period, then Add Part once more and choose Extension.

FilesAutomator
The rename pattern: a Find and Replace trimming a trailing space, a period, then the extension
The rename pattern: a Find and Replace trimming a trailing space, a period, then the extension

Turn it on

Select Create Automation Rule, then switch the rule on. Every file that lands here now has that stray trailing space trimmed automatically.

A faster way

The template gallery inside FilesAutomator already has this rule ready to install, named Remove trailing spaces from filenames.

← Back to all automation rules

Get FilesAutomator

Everything in this guide happens inside FilesAutomator itself. Download it free from the Mac App Store to follow along, or to install this exact rule from the template gallery in one click instead of building it by hand.

Download on the App Store