how to replace spaces with hyphens in filenames on Mac

If you work with the web at all, you probably know that having spaces in a filename online is not a good idea. You can do it, but every space gets replaced with “%20” in the file’s URL. So how can you replace spaces with hyphens easily?

So an image file named “image name with spaces.jpg” becomes “image%20name%20with%20spaces.jpg” when uploaded to a website. The same is true for all image file formats (.png, .gif, etc.) as well as all document formats (.doc, .pdf, etc.). Any file uploaded and linked will have this issue if it has spaces.

Is it the end of the world? No. But if you work in the web world, even if just a personal blog, take the time to remove the spaces.

What character should I use instead of a space?

Most people chose to use either a hyphen ( – ) or and underscore ( _ ) instead of a space. I prefer to use a hyphen, and if you look around you’ll notice the best practice is to use hyphens. There is actually a reason for this. The hyphen is preferred by most search engines as it helps their algorithms determine the relevance of a file for search purposes. So, using a hyphen is preferred over spaces, and even over just mashing filenames into one long word (e.g., image-name-without-spaces.jpg is better than imagenamewithoutspaces.jpg for both humans and algorithms). 

To be clear, the hyphen (sometimes called dash) is the little line on the top right of the US English keyboard ( – ) that is also used as the minus sign. There is actually a separate character that is a “dash” that is longer than the hyphen. But in most cases people use these terms interchangeably. 

How to replace spaces with hyphens in filenames.

The manual method is is to open your file browser (Finder on Mac, File Explorer on Windows), find the file you need to change, and then right click and select “rename”. Then you can manually delete and replace the spaces with hyphens. 

But it is a tedious process, especially if you have multiple filenames to edit. It takes multiple clicks and is highly inefficient. 

Replace spaces with hyphens in bulk on a Mac with Automator.

The good news is that replacing spaces with hyphens can be easy to do, even in batches, if you have an Apple MacIntosh computer. (I am sure there is a relatively simple way to do it on Windows, but I am not aware of a built-in utility to do so.) 

All modern Mac’s (MacBooks, MacBook Pros, iMacs, Mac Pros, etc.) have a built in application called Automator that will allow you to build a simple tool to replace spaces in file names and replace them with whatever character you choose. In my case, I have it set up to allow me to find the files in Finder and drag them to a special icon on my desktop. The rest happens automagically!

Fortunately, you don’t have to be a programmer to use Automator. 

Here is a step-by-step tutorial on how to build your own Automator workflow for changing spaces to hyphens in filenames.

1. Open Automator and select “Application”.

There are other options here that I won’t go into (you can research those on your own). I recommend using “application” and saving it to your desktop (explained later). An application is essentially a file that does whatever you build it to do. In our case, once it is built, we will locate the files we need to alter and simply drag and drop them on the Application.

How to replace spaces with hyphens in filenames on a Mac - automator application

2. Click on “files and folders” and then drag “rename finder items” to the pane on the right. 

The right pane is where you build your workflow (If you are confused, it’s the one that says “Drag actions or files here to build your workflow.”). Notice that there are plenty of “Actions” you can choose from. We won’t go into those in this post, but Automator can be used to do many, many things. Dragging this action to the right side will give you a prompt…which is the next step. 

replace spaces with hyphens in filenames on a Mac - select rename

3. Optional – You will be prompted to add a “copy finder items action”. 

Many of these possible actions you can choose from alter the file in some way, so Apple is reminding you that, depending on what you want to accomplish, you may want to add the “copy” action as well. In this case, we are making a simple workflow with one step, so you probably don’t need to copy first.

optional prompt

4. In the first drop down, select “replace text”. 

This is where you want to get things right. Select “replace text” in the first drop down. As you can see, there are several things you can do to filenames using this same “action”. At this point we’ll just “replace text”, but you can experiment with other workflow actions as well. 

replace spaces with hyphens in filenames on a Mac - replace text drop down

5. In the “Find” box, type one space. In the “replace” box, type a hyphen.

This is where the magic happens. In the “find” box be sure you put in a single space. Since you can’t see spaces, you can highlight the text in the box to verify something is there (as I have done in this example).

In the “replace” box put only a das (hyphen) and nothing else. You could use a different character, but as mentioned above, a hyphen is the preferred option. 

replace spaces with hyphens - be sure to use one space and the hyphen

6. Save the “application”, give it a name you’ll recognize, and save it to your desktop.

Go to the File menu and click “save” or type command + s. I save my Automator applications to my desktop for quick access, but you could save it anywhere you want. Now that you have saved your workflow application, you can leave Automator open while you test it in case you discover you need to fix something. 

name it something like replace spaces with hyphens

7. Test your Automator application.

Now it is time to test your creation, but you need a file (or files) to test it with. 

You have a couple options. You could browse something like your “downloads” folder for a file that already has spaces in it, or you could copy several files to a temporary folder and rename them so that they all have at least one space in their file name. It doesn’t really matter what kind of file they are, but images are easy to work with. 

Open a finder window with the files you want to change. Select them, drag and drop them on the application icon on your desktop. And that’s it! The file names should have any spaces replaced with hyphens (or underscore, or whatever character you chose in the previous steps). 

before and after of file names
The image on the left shows 10 files with spaces in their names. Once they are dragged and dropped to the “replaces spaces with hyphens.app”, the result is all spaces replaced with hyphens (image on the right).

If everything worked, you are set. If not, look back at your workflow and figure out what went wrong.

Any time you need to replace spaces with hyphens in a filename, all you have to do is drop and drop it onto your Automator application! You can do it with just one file, or with dozens. It doesn’t matter. 

Happy Automating!