Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

11
  • 11
    Simple and work in mac. (mac doesnt have rename, and its too hard to install this with brew..)
    – JohnnyJS
    Commented Nov 30, 2014 at 12:33
  • 7
    awesome answer. i used for d in *\ *; do mv "$d" "${d// /}"; done non under score.
    – Yoon Lee
    Commented Feb 24, 2015 at 6:32
  • 2
    For reference, this can easily become recursive in bash for using shopt -s globstar and for f in **/*\ *; do .... The globstar option is internal to bash, whereas the rename command is a common Linux tool and not part of bash.
    – ghoti
    Commented Dec 5, 2016 at 21:12
  • 6
    ${f// /_} is a Bash variable expansion for search and replace. - The f is the variable from the for loop for each file that contains a space. - The first // means "replace all" (don't stop at first occurrence). - Then the ` /_` means "replace space with underscore"
    – Ari
    Commented May 19, 2020 at 0:53
  • 2
    based on your solution, I made it recursive: while read line ; do mv "$line" "${line// /}" ; done < <(find /path/ -iname "* *") Commented May 14, 2021 at 20:19