Timeline for How to replace spaces in file names using a bash script
Current License: CC BY-SA 4.0
15 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 18, 2023 at 21:07 | comment | added | Naidim | Any files with a space. * are wildcards and \ escapes the space | |
May 10, 2022 at 0:46 | comment | added | mtk | What does '*\ *' mean? | |
Aug 24, 2021 at 13:36 | comment | added | Al Lelopath |
I wanted to remove spaces (as opposed to replacing with _). Removing the underscore in the statement worked: for f in *\ *; do mv "$f" "${f// /}"; done
|
|
May 14, 2021 at 20:19 | comment | added | Ivan Apolonio |
based on your solution, I made it recursive: while read line ; do mv "$line" "${line// /}" ; done < <(find /path/ -iname "* *")
|
|
Jun 5, 2020 at 15:26 | comment | added | evaristegd | @Ari , thank you so much for explaning all three slashes! The original answer only explains two slashes, which I find very inconvenient. | |
May 19, 2020 at 0:53 | comment | added | Ari |
${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"
|
|
Mar 5, 2019 at 8:47 | history | edited | hoijui | CC BY-SA 4.0 |
use code formatting
|
S Nov 14, 2017 at 11:39 | history | suggested | dr_agon | CC BY-SA 3.0 |
Added explanation of the part "${f// /_}" as requested in some questions.
|
Nov 14, 2017 at 10:10 | review | Suggested edits | |||
S Nov 14, 2017 at 11:39 | |||||
Jul 17, 2017 at 6:14 | comment | added | Mukit09 | worked for me... plus one... Can anyone answer me how "${f// /_}" does this part work? | |
Dec 5, 2016 at 21:12 | comment | added | ghoti |
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.
|
|
Jan 12, 2016 at 14:26 | comment | added | Julio Faerman | Unlike the 'find -name' answer, this one worked on my OS X! Thank you sir! | |
Feb 24, 2015 at 6:32 | comment | added | Yoon Lee |
awesome answer. i used for d in *\ *; do mv "$d" "${d// /}"; done non under score.
|
|
Nov 30, 2014 at 12:33 | comment | added | JohnnyJS |
Simple and work in mac. (mac doesnt have rename , and its too hard to install this with brew..)
|
|
Aug 13, 2013 at 15:23 | history | answered | Naidim | CC BY-SA 3.0 |