Last Updated: October 01, 2016
· superboum

Sanitize some files with bash

Here is the script that I used to rename my audio files before writing them on a CD.
Some parts are not very clean. Particularly the extension removal plus the static folder name (musique).
Don't expect to use this script "as is".

shopt -s extglob # Needed to use more powerful regexp
ls musique/ | {
    while read filename
        newfilename=${filename::-4} # remove .mp3
        newfilename=${newfilename//+([^A-Za-z0-9])/_} # replace non alphanumerics characters by underscore
        newfilename=${newfilename:0:26} # limit length of the file
        echo "$newfilename.mp3" # new name
        mv "musique/$filename" "musique/$newfilename.mp3" # Move the file. You should comment it first...
Filed Under