Bash – replace a ‘space’ char in filename with an underscore

bash

I have a bunch of files in a directory with 'spaces' in the filename.

How do I perform a bulk rename of all filenames with 'spaces' and replace them with an '_' char.

Looking at the other solutions, I've tried the following command w/o success:

find . -name '* *' -exec rename ' ' '_' {} +

find: rename: No such file or directory

Best Answer

Try:

$ for file in *; do [ -f "$file" ] && ( mv "$file" "$(echo $file | sed -e 's/ /_/g')" ); done