How to remove spaces and special characters from many file names

A folder of files called <code>Final Report (v2).pdf</code> and <code>Café menu #3.png</code> works on your desktop, then a script splits the names at the spaces, a link shows <code>%20</code>, or an upload rejects the <code>#</code>.

Why spaces and symbols cause trouble

A sensible naming rule

BeforeAfterWhat changed
Final Report (v2).pdffinal-report-v2.pdfspaces to dashes, brackets removed, lowercase
Café menu #3.pngcafe-menu-3.pngaccent dropped, # removed
Q3: budget?.xlsxq3-budget.xlsxWindows-illegal characters removed

Dashes or underscores both work; pick one and keep to it. Search engines treat dashes in web addresses as word breaks, so dashes are the usual choice for files that end up online.

Clean a whole folder with a preview

  1. Load the names into the batch rename tool.
  2. Turn on spaces to -, remove Windows-illegal characters and lowercase. For other symbols, use find and replace with a regular expression such as [^A-Za-z0-9._-] replaced with nothing.
  3. Check the table: if notes (1).txt and notes 1.txt both become notes-1.txt, it is marked as a collision and the script is held back until you fix it.
  4. Copy the bash or PowerShell script and the undo script, then run it in the folder.

Quick one-liners

for f in *\ *; do mv -n -- "$f" "${f// /-}"; done
Get-ChildItem -File | Where-Object Name -match ' ' |
  Rename-Item -NewName { $_.Name -replace ' ', '-' }

Neither checks for collisions in advance: mv -n skips a file whose new name already exists, and Rename-Item stops with an error, so look at what was left behind. To turn a single title into a dashed lowercase name, the case converter has a kebab-case option, and the URL encoder shows what a name with spaces turns into inside a link.

Related

How to batch rename files safely (without uploading them) · Why a batch rename overwrites or skips files (name collisions) · Why file10 sorts before file2, and how to add leading zeros · Renaming a file when only the upper/lower case changes · Windows names you cannot use: CON, NUL, AUX, COM1 · Batch rename tool (names only, in your browser) · Compress many photos at once · Invisible characters in text

Questions

Which characters are safe in file names?

Letters a to z, digits 0 to 9, dash, underscore and dot are safe on every common system and in web addresses. Lowercase avoids case problems between Windows, macOS and Linux.

Should I use dashes or underscores instead of spaces?

Either works. Dashes are the usual choice for files that end up on the web, because search engines read a dash as a word break.

Why does my file name show %20 in a link?

Spaces are not allowed in web addresses, so they are encoded as %20. Replacing spaces with dashes gives a cleaner link.

How do I remove spaces from many file names at once?

Use the batch rename tool's spaces rule and check the preview, or run for f in *\ *; do mv -n -- "$f" "${f// /-}"; done in bash.

Can cleaning names make two files the same?

Yes. notes (1).txt and notes 1.txt can both become notes-1.txt. The batch rename tool marks those rows as collisions before anything is renamed.

Published .

Try it

Related guides

Newest guides