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
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Highlight Table Rows, Columns, and Cells Using MooTools 1.2.3

    Row highlighting and individual cell highlighting in tables is pretty simple in every browser that supports :hover on all elements (basically everything except IE6). Column highlighting is a bit more difficult. Luckily MooTools 1.2.3 makes the process easy. The XHTML A normal table. The cells...

  • By
    Create Digg URLs Using PHP

    Digg recently came out with a sweet new feature that allows users to create Tiny Digg URLs which show a Digg banner at the top allowing easy access to vote for the article from the page. While I love visiting Digg every once in a...

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!