How to Remove Spaces from Filenames on Mac

Spaces in filenames create friction in Terminal commands, shell scripts, URLs, and cross platform workflows. Removing or replacing spaces with underscores, hyphens, or nothing eliminates these issues. This guide covers three macOS methods for cleaning spaces from filenames in bulk.

Why Are Spaces in Filenames a Problem?

Spaces in filenames break Terminal commands, require escaping in shell scripts, encode as %20 in URLs, and cause errors in automated pipelines. Terminal treats spaces as argument separators, so a file named "my report.pdf" is interpreted as two separate arguments: "my" and "report.pdf" unless properly quoted.

Terminal is the most common source of space related filename issues. Every Terminal command that references a file with spaces requires quoting the filename with double quotes or escaping each space with a backslash. Forgetting to quote a filename in a script can produce errors, process the wrong files, or create unintended new files. When processing hundreds of files in a loop, a single unquoted variable containing spaces can corrupt the entire batch operation.

Web servers encode spaces as %20 in URLs, making links longer and harder to share. Content management systems, FTP clients, and cloud storage platforms handle spaces inconsistently across operating systems. A file created on macOS with spaces may arrive on a Linux server with different escape characters. Removing spaces before uploading or sharing files prevents these compatibility issues across platforms. See the special characters guide for removing other problematic characters.

How Do You Remove Spaces from Filenames in Finder?

Finder removes spaces from filenames using its Replace Text rename mode. Select the files, right click, choose Rename, select Replace Text, type a single space in the Find field, leave the Replace field empty to remove spaces entirely, and click Rename. Finder removes every space from every selected filename in one operation.

Finder's Replace Text mode performs a straightforward string replacement on all selected filenames. Typing a space in the Find field and leaving the Replace field empty strips all spaces from the filenames. Typing an underscore in the Replace field converts every space to an underscore instead. The operation applies to all selected files simultaneously with no per file control.

Finder's Replace Text mode has limitations for space removal. It cannot replace only leading or trailing spaces while preserving internal spaces. It cannot replace multiple consecutive spaces with a single underscore. It cannot apply different replacement rules based on the space position within the filename. For these advanced operations, Terminal or Batchio provides the necessary control through regex patterns and dedicated character removal rules.

How Do You Replace Spaces with Underscores on Mac?

Terminal replaces spaces with underscores using the mv command with parameter substitution: for f in *\ *; do mv "$f" "${f// /_}"; done. This command iterates through files containing spaces and replaces every space with an underscore. Finder's Replace Text mode also handles this by typing a space in Find and an underscore in Replace.

The Terminal approach uses bash parameter expansion where ${f// /_} replaces all occurrences of a space with an underscore in the variable f. The glob pattern *\ * matches only files that contain at least one space, so files without spaces are skipped automatically. This command processes all matching files in the current directory. Adding find with -exec extends the operation to subdirectories.

Underscores are the preferred replacement for spaces in programming contexts. Python, JavaScript, Ruby, and most programming languages use underscores in identifiers. Shell scripts handle underscores without any special escaping. Batchio's find and replace rule handles space to underscore conversion with a simple search for a space and replacement with an underscore. The remove characters rule can strip spaces entirely without needing a replacement string.

How Does Batchio Remove or Replace Spaces in Filenames?

Batchio removes spaces through two rule types. The find and replace rule searches for spaces and replaces them with any character. The remove characters rule strips spaces entirely with a single toggle. Both rules update the live preview in real time.

Batchio's remove characters rule provides dedicated controls for common character removal operations. Toggling the spaces option strips all space characters from every filename in the batch. The live preview shows the original and cleaned filenames side by side, so you can verify that removing spaces produces readable results before committing the operation.

The find and replace rule offers more control when you want to replace spaces instead of removing them. Enter a space in the search field and an underscore, hyphen, or any other character in the replacement field. Enable regex mode to handle advanced patterns like replacing multiple consecutive spaces with a single underscore. Stack additional rules to clean other characters in the same pass. See the batch rename guide for combining multiple rules in a single operation.

Can You Remove Spaces from Filenames in an Entire Folder?

Finder processes only the files you select in the current folder view. Terminal commands with find recurse through subfolders to remove spaces from filenames across entire directory trees. Batchio supports subfolder scanning with a single toggle, applying space removal rules to every file in the folder hierarchy.

Recursive space removal is common when cleaning up file collections downloaded from the web, imported from external drives, or received from collaborators using different naming conventions. Terminal's find command with -depth processes files from the deepest subdirectory first to avoid path conflicts when parent folder names also contain spaces. The command find . -depth -name "* *" -execdir rename 's/ /_/g' {} \; handles the entire tree in one pass.

Batchio provides a subfolder toggle in the toolbar that includes all files from nested directories in the file list. The space removal rule applies to every file regardless of its depth in the folder hierarchy. The live preview shows the full path for each file, making it easy to verify that the operation produces correct results at every level. Undo support reverts the entire recursive operation with a single Cmd+Z if any filename needs to be restored.

Frequently Asked Questions

Why do spaces in filenames cause problems on Mac?
Spaces in filenames cause problems in Terminal commands, shell scripts, URLs, and cross platform file transfers. Terminal interprets spaces as argument separators, so filenames with spaces must be quoted or escaped. URLs encode spaces as %20, which makes links harder to read and share. Some Linux and Windows tools mishandle files with spaces in automated pipelines.
Can Finder remove spaces from multiple filenames at once?
Finder can replace spaces with other characters using its Replace Text rename mode. Select the files, right click, choose Rename, select Replace Text, type a space in the Find field, and type the replacement character (underscore, nothing, or another character) in the Replace field. Finder applies the replacement to every selected file.
Should you replace spaces with underscores or hyphens?
Underscores are the standard choice for programming and scripting contexts because they do not require escaping in Terminal or shell scripts. Hyphens are preferred for web URLs because search engines treat hyphens as word separators. Choose based on your primary use case: underscores for code and scripts, hyphens for web content.
Does removing spaces from filenames affect file contents?
Removing spaces from filenames only changes the name displayed in Finder and referenced by the file system. The file contents, internal metadata, creation date, and modification date remain completely unchanged. Only the name and any hardcoded references to that name in other files are affected.

Clean Up Filenames in Seconds

Download Batchio free on the Mac App Store. All 9 rule types included. Pro upgrade $4.99.

Coming Soon to the Mac App Store
Marcel Iseli
Marcel Iseli

Creator of Batchio · Indie App Developer

LinkedIn ↗

Marcel Iseli is an indie app developer and the creator of Batchio. He builds native macOS utilities focused on productivity and file management, with a focus on lightweight, subscription-free tools.