Last Updated: February 25, 2016
·
1.046K
· gautamnitish

Remove Whitespace From All Filenames in the current directory

http://www.gautamk.com/blog/2012/09/07/remove-whitespace-from-all-filenames/

Bash :

for i in * ; do 
    if [ $i != ${i//[[:space:]]} ] ; then
        mv $i ${i//[[:space:]]}
    fi
done

Another one by chepner

for old in *; do
    new="${old//[[:space:]]}"
    [[ $old = $new || -f $new ]] || mv "$old" "$new"
done

Python :

import os
for f in os.listdir("."):
    r = f.replace(" ","")
    if( r != f):
        os.rename(f,r)

Here is the related stackoverflow question