Tuesday, August 9, 2022
spot_imgspot_img
HomeFranchise DirectoriesHow to replace one char with another in all filenames of the...

How to replace one char with another in all filenames of the current directories?

In any shell, you can loop over the files whose name contains a space. Replacing the spaces with underscores is easy in bash, ksh and zsh with the ${VARIABLE//PATTERN/REPLACEMENT} construct.

for x in *” “*; do mv — “$x” “${x// /_}” done

On Debian, Ubuntu and derivatives, you can use the Perl rename (other distributions ship a different program as rename, and that program isn’t helpful here).

rename ‘s/ /_/g’ *

An obligatory zsh solution:

autoload zmv zmv ‘(*)’ ‘${1// /_}’

An obligatory POSIX solution:

for x in *” “*; do y=$(printf %sa “$x” | tr ” ” “_”) mv — “$x” “${y%a}” done

Most Popular