Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change spaces with underscore in filename

I have a multiples files with space, and I want replace this for a underscore with command line (Mac)

xxx xxx.jpg -> xxx_xxx.jpg

It's possible with only line command?

like image 422
NARTONIC Avatar asked Dec 06 '22 17:12

NARTONIC


1 Answers

For all files in current directory

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

If you only want to match files with spaces (to prevent tons of error messages when it tries to move files into themselves) though you could use extended glob

shopt -s extglob
for i in +(* *);do mv "$i" "${i// /_}";done
like image 199
123 Avatar answered Dec 27 '22 04:12

123