Removing spaces from filenames

If you use bash under unix, Linux, or Mac OS X, and you’re continually forgetting how to convert filenames with spaces to filenames without spaces, then look no further.

I use dash (-) for the final space replacement, as it looks sharp IMO.

find ./ -name '* *' -type f -print0 | \
while IFS= read -r -d $'\0' file; do 
 echo "$file" "${file// /-}"; 
done

I figured if it takes me a few minutes each time to figure it out, it’s worth documenting. And, if it’s worth documenting, it’s worth sharing with others. I hope this was useful. If it is, please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *