Remove Spaces from File Names

By  on  

Spaces in file names are a nightmare with the web; you deal with %20 and other nonsense when spaces are in file names.  That's why when I receive images with spaces I cringe;  I mean hell, dealing with spaces of file systems sucks too.

Anyways, I use the following command to remove spaces in file names within a directory:

# Replace spaces in file names with "-"
for f in *\ *; do mv "$f" "${f// /-}"; done

In this case I replace spaces with a dash.  You can omit the dash if you'd rather there be nothing in place of spaces; underscores is another common pattern.

This script is not recursive, so it wont dive into subdirectories -- I don't usually have that case.

Recent Features

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

  • By
    iPhone Checkboxes Using MooTools

    One of the sweet user interface enhancements provided by Apple's iPhone is their checkbox-slider functionality. Thomas Reynolds recently released a jQuery plugin that allows you to make your checkboxes look like iPhone sliders. Here's how to implement that functionality using the beloved...

Discussion

  1. Danny

    Is there a way to reverse this process? I get file names from the web that have no spaces. I want to put them back in. For example: HowToCookATurkey.txt would return to How To Cook A Turkey.txt

    I haven’t found any of the renaming programs out there that tell you how to do it. Thanks in advance.

  2. h

    you’re a lifesaver! thx!

  3. JD

    New to coding so need some help. Trying to figure out how to edit you code to work for my issue. Have a file, “Outfile-ABC-123 456 789.txt” in a directory, “//dev/out/file/” which has empty spaces in the file name which I need to replace the them with “0” or delete them all together whichever is simpler. Is the code below correct to help resolve my issue? Thanks in advance.

    Before: “Outfile-ABC-123 456 789.txt”

    After: “Outfile-ABC-12304560789.txt” or “Outfile-ABC-123456789.txt”

    My version of your code edited.

    for “Outfile-ABC-*” in (“\\Dev\Out\File\”;) do (mv “$Outfile-ABC-*” “${Outfile-ABC-*// /0}”); done

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!