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
- Command lines split on spaces.
cp My File.txt backup/sees two files,MyandFile.txt. Every script must quote names, and many do not. - URLs escape them. A space becomes
%20,#starts a fragment and cuts the link short,?starts a query, and%itself must become%25. - Some systems reject them. Windows refuses
< > : " / \ | ? *; some upload forms, build tools and older software are stricter still. - Accents can vary invisibly.
écan be stored as one character or aseplus a combining accent (macOS often uses the second form), so two names can look identical and still differ.
A sensible naming rule
| Before | After | What changed |
|---|---|---|
Final Report (v2).pdf | final-report-v2.pdf | spaces to dashes, brackets removed, lowercase |
Café menu #3.png | cafe-menu-3.png | accent dropped, # removed |
Q3: budget?.xlsx | q3-budget.xlsx | Windows-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
- Load the names into the batch rename tool.
- 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. - Check the table: if
notes (1).txtandnotes 1.txtboth becomenotes-1.txt, it is marked as a collision and the script is held back until you fix it. - 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 .