Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save windsting/c3943be1a9879b0e1afdd2234e4030aa to your computer and use it in GitHub Desktop.
Save windsting/c3943be1a9879b0e1afdd2234e4030aa to your computer and use it in GitHub Desktop.
How to replace the spaces in filenames with underscore

How to replace the spaces in filenames with underscore

TL;DR

for name in *; do mv "$name" "${name// /_}"; done

Source

How to replace the spaces in filenames with underscore [duplicate]: https://unix.stackexchange.com/questions/321448/how-to-replace-the-spaces-in-filenames-with-underscore/321456

Origin reply

Into bash for all the files into folder.

for name in *; do mv "$name" "${name// /_}"; done

The ${name/pattern/replace} replaces pattern to replace (Bash Parameter Expansion). If pattern starts with / (here pattern is / + Space), it replaces all the occurencies. Then mv renames file from name to new name with replaced spaces.

edited Nov 6 '16 at 15:52 and answered Nov 6 '16 at 15:46 by Konstantin Morenko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment