Search code examples
regexlinuxubunturm

How to delete files that are not mp3?


I have a mp3 Player. I have connected it to my PC which has Ubuntu as an OS. I want to delete all the files that are not .mp3.

I know that for deleting them the command is rm *.mp3, but what's the combination for negating this? I've tried: rm ^.mp3, ^mp3 ^[mp3], but they didn't work! Any suggestion?


Solution

  • Assuming you are using bash and extglob is set, you should be able to:

    rm !(*.mp3)
    

    I would try a test with ls before blindly running that command:

    ls !(*.mp3)
    

    To check the value of extglob:

    shopt extglob
    

    And to turn it on, if necessary:

    shopt -s extglob