Wednesday, March 31, 2010

Rename files with shell script

Rename a large list of files with names containing spaces and different characters

All the job is done using the tr command.


The following script i found on a Archlinux Forum Post changes spaces to underscores, remove characters like {}(),\! and convert the filenames to lowercase.

#!/bin/bash
ls | while read -r FILE
do
mv -v "$FILE" `echo $FILE | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d "\'" | tr '[A-Z]' '[a-z]' | sed 's/_-_/_/g'`
done

The following script just changes spaces to underscores.

#!/bin/bash
ls | while read -r FILE
do
mv -v "$FILE" `echo $FILE | tr ' ' '_' `
done



Great everyday scripts. Thanks paramthegreat (Article post)