Spaces in File Names: Spaces vs Hyphens vs Underscores (and How to Replace Them)
Spaces are allowed in file names on Windows, macOS and Linux, and for files that only you open by double-clicking they are fine. They cause trouble as soon as a file goes into a URL (a space becomes %20), a command line or script (the space splits the name into two arguments unless you quote it), or certain upload and sync tools. For anything shared, published or scripted, use hyphens instead (quarterly-report.pdf), or underscores if your team prefers them.
This guide explains exactly what goes wrong with spaces, when to choose hyphens (a "dashed filename") or underscores, and how to replace spaces in many file names at once on Windows, Mac and Linux.
What goes wrong with spaces in file names
In URLs: spaces become %20
A URL cannot contain a literal space, so browsers and servers percent-encode it as %20. A file uploaded as Annual Report 2026.pdf ends up at an address like this:
https://example.com/files/Annual%20Report%202026.pdfIt still works, but the link is ugly, hard to read aloud or type, and easy to break when someone copies part of it or pastes it into an email that wraps at the space. In form data and some query strings a space can also be encoded as +, which adds more room for mismatches. A dashed filename gives you a clean URL with nothing to encode:
https://example.com/files/annual-report-2026.pdfOn the command line: the name splits in two
Shells such as bash, zsh, PowerShell and Command Prompt use spaces to separate arguments. This command does not copy one file, it tries to copy two files called Annual and Report.pdf:
cp Annual Report.pdf backup/You have to quote the name or escape each space:
cp "Annual Report.pdf" backup/
cp Annual\ Report.pdf backup/Quoting is easy to remember when you type one command, and easy to forget inside a script, a loop or a build tool. Unquoted variables in shell scripts are a classic source of bugs that only appear when someone adds a file with a space in its name.
In other tools
Some older software, email gateways, FTP setups and build systems still handle spaces badly. Problems are rarer than they used to be, but a file name without spaces never triggers them. Leading or trailing spaces are worse still: they are invisible, so report.pdf and report.pdf look identical. File Explorer strips a trailing space when you rename a file, but such names can still arrive from other systems.
Spaces vs hyphens vs underscores
| Spaces | Hyphens (dashes) | Underscores | |
|---|---|---|---|
| Example | client report.pdf | client-report.pdf | client_report.pdf |
| Readable by people | Best | Good | Good |
| Clean in URLs | No (%20) | Yes | Yes |
| Command line without quotes | No | Yes | Yes |
| Recommended by Google for URLs | No | Yes | No |
| Double-click selects whole name | No | Usually no | Yes |
Hyphens: best for anything on the web
Google's URL structure guidelines recommend hyphens rather than underscores to separate words in URLs, because hyphens clearly signal a word boundary. If a file name becomes part of a URL (images, PDFs, downloads on a website), blue-running-shoes.jpg is the right form. Our guide to image file names for SEO goes into this further.
Underscores: fine for internal files and code
Underscores are just as safe on the command line and in URLs, and they have one practical advantage: double-clicking a word in most editors and terminals selects the whole client_report_2026 name, while hyphens often break the selection at each dash. Programmers also use underscores because a hyphen means minus in most languages.
A combined convention
Many teams use both, with different jobs: underscores between the parts of a name and hyphens inside a part.
2026-11-08_acme-corp_invoice-0142.pdfHere the date, client and document fields are separated by _ and each field uses - between words. You can read it, sort it, and split it back into fields with a script. The naming conventions guide explains how to choose and document a scheme like this.
Should you ever keep spaces?
Yes, for personal documents that never leave your computer and that you only open from Finder or File Explorer. "Tax Return 2025.pdf" in your own Documents folder will never bother anyone. The moment a folder is shared, synced to a web app, served from a website or processed by a script, switch to hyphens or underscores.
How to replace spaces in file names in bulk
Changing a few names by hand is quick (see how to rename a file). For a folder full of them, use one of the methods below. Try them on a copy of the folder first.
Windows: PowerShell
Open PowerShell in the folder (in File Explorer, right-click an empty area and choose Open in Terminal) and preview the change with -WhatIf:
Get-ChildItem -File | Where-Object Name -like '* *' | Rename-Item -NewName { $_.Name -replace ' ', '-' } -WhatIfIf the preview looks right, run it again without -WhatIf. To use underscores, change '-' to '_'. To collapse runs of several spaces into one dash, use the regular expression ' +' instead of ' '. Add -Recurse after Get-ChildItem to include subfolders. Our PowerShell bulk rename guide has more patterns.
If you prefer a graphical tool, Microsoft's free PowerToys includes PowerRename: select the files, right-click, choose Rename with PowerRename, search for a space and replace it with a hyphen.
Mac: Finder's Replace Text
Finder can do this without any extra software:
- Select the files in Finder.
- Control-click one of them and choose Rename.
- In the pop-up menu, choose Replace Text.
- In Find, type a single space. In Replace with, type
-or_. - Check the example at the bottom of the dialog and click Rename.
Finder replaces every space in each selected name. The Mac bulk rename guide covers the other two modes, Add Text and Format.
Linux and Mac Terminal: a shell loop
This bash loop renames every file and folder in the current directory that has a space, replacing each space with a hyphen. -n stops mv from overwriting a file that already has the new name, and -- protects names that start with a dash:
for f in *\ *; do mv -n -- "$f" "${f// /-}"; doneTo preview first, put echo in front of mv. For underscores, use "${f// /_}". The same loop works in zsh, the default shell on macOS.
To include subfolders, let find walk the tree from the deepest level up, so folders are renamed after the files inside them:
find . -depth -name '* *' -execdir bash -c 'mv -n -- "$1" "${1// /-}"' _ {} \;Both commands were tested with bash on Ubuntu 24.04. Note that a name with two spaces in a row becomes two dashes (two--spaces.txt); the Linux rename guide shows how to tidy that with the rename command.
Any system: in the browser
Our free online bulk renamer has a replace-spaces rule as well as find and replace, case changes (including kebab-case and snake_case) and a live preview. It runs entirely in your browser, so files are not uploaded, and you download the renamed copies as a ZIP.
Other characters to avoid while you're at it
Spaces are the most common problem, but the same clean-up should cover:
- Characters Windows forbids:
\ / : * ? " < > |. Files with these names, created on a Mac or Linux machine, cause errors when copied to Windows or synced to OneDrive. - Ampersands, hashes, percent signs and plus signs: they have special meanings in URLs (
#starts a fragment,%starts an encoded character). - Accented letters and emoji: usually fine on modern systems, but they get percent-encoded in URLs and can be stored in different Unicode forms on Mac and Windows, so a name can look identical but not match.
- Leading dashes and dots: a name like
-notes.txtis read as an option by command-line tools, and a leading dot hides the file on Mac and Linux.
When the names themselves are the problem
Swapping spaces for hyphens tidies a name but doesn't make Scan 0042.pdf any more useful as Scan-0042.pdf. If your files have generic camera or scanner names, File Renamer AI reads each photo, PDF or document and suggests a descriptive name with hyphens or underscores in whatever case you choose, which you can review before downloading.
Frequently Asked Questions
Is it bad to have spaces in file names?
Not for personal files you open by double-clicking. Spaces cause problems when a file name goes into a URL, where each space becomes %20, or into a command line or script, where the name must be quoted. For shared, web or scripted files, use hyphens or underscores instead.
Should I use hyphens or underscores in file names?
Use hyphens for anything that ends up on the web, since Google recommends hyphens to separate words in URLs. Underscores work equally well for internal files and code. Pick one and use it consistently; some teams use underscores between fields and hyphens within them.
What is a dashed filename?
A dashed filename uses hyphens instead of spaces between words, such as annual-report-2026.pdf. It is also called kebab-case. Dashed names are readable, need no quoting on the command line, and produce clean URLs.
How do I open a file with spaces in the name from the command line?
Put the whole name in quotes, for example open "Annual Report.pdf" on a Mac or start "" "Annual Report.pdf" in Command Prompt, or escape each space with a backslash in bash and zsh. Pressing Tab to auto-complete the name adds the escaping for you.
How do I remove spaces from many file names at once?
On Windows use PowerShell with Rename-Item and -replace, or PowerToys PowerRename. On a Mac select the files, choose Rename, then Replace Text with a space in Find and a hyphen in Replace with. On Linux use a bash loop with mv. A browser-based bulk renamer also works on any system.
Why does my file name show %20 instead of a space?
%20 is the URL encoding for a space. When a file with spaces in its name is uploaded to a website or shared by link, each space is converted to %20 so the address is valid. Renaming the file with hyphens before uploading avoids it.
Related Posts
Image File Names for SEO: How to Name Images (With Examples)
How to name images for SEO: short, descriptive, hyphenated file names, how they differ from alt text, what WordPress and Shopify do, and how to rename safely.
AdvancedHow to Rename a GitHub Repository (and What Breaks)
Rename a GitHub repository in Settings > General, then update your local remote with git remote set-url. What redirects, and what breaks: Pages and Actions.
TutorialHow to Rename Photos on iPhone (and What the Photos App Can’t Do)
How to rename photos on iPhone: why the Photos app keeps IMG_ names, how to rename a photo in the Files app, and how to rename many pictures at once.
Ready to organize your files?
Try AI renaming right away. No sign-up needed until you download.
Try It Free