kl9xhq
Last Updated: October 01, 2016
·
49
· superboum
Avatar1

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".

#!/bin/bash
shopt -s extglob # Needed to use more powerful regexp
ls musique/ | {
    while read filename
    do
        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...
    done
}
Say Thanks
Respond
Filed Under