Search code examples
console

Batch rename files if they don't contain a character


I have audio files names either 'A_B.wav' or 'A.wav'. I'd like to rename all files with a filename in the format 'A.wav' to 'A_A.wav' and leave all other files unchanged.

In other words, I need to batch rename only files that contain no underscore.

Is there a way to do this via the Linux console?


Solution

  • #!/bin/bash
    
    for i in *;
    do
        j=`echo $i | cut -d . -f 1`;
        e=`echo $i | cut -d . -f 2`;
        if [[ $j != *_* ]]
        then
            j=$j"_"$j"."$e;
            mv $i $j;
        fi
    done